Questions tagged [pytest]

For questions about the pytest Python testing tool. Please also add the [python] tag for questions tagged with [pytest].

pytest is a mature, fully featured testing tool that simplifies the testing experience, which:

  • Provides no-boilerplate testing.
  • Supports functional testing and complex test setups.
  • Integrates many common testing methods.
  • Offers extensive plugin and customization system.

Be sure to check the Getting Started docs to see how easy it is to get running with pytest. The docs are comprehensive and development is open to contributions.

9822 questions
60
votes
5 answers

In which order are pytest fixtures executed?

For an application I'm testing I'd like to create an autouse=True fixture which monkeypatches smtplib.SMTP.connect to fail tests if they try to send an email unexpectedly. However, in cases where I do expect tests to send emails, I want to use a…
ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
60
votes
9 answers

py.test: how to get the current test's name from the setup method?

I am using py.test and wonder if/how it is possible to retrieve the name of the currently executed test within the setup method that is invoked before running each test. Consider this code: class TestSomething(object): def setup(self): …
Dr. Jan-Philip Gehrcke
  • 33,287
  • 14
  • 85
  • 130
59
votes
5 answers

Pytest: Deselecting tests

With pytest, one can mark tests using a decorator @pytest.mark.slow def some_slow_test(): pass Then, from the command line, one can tell pytest to skip the tests marked "slow" pytest -k-slow If I have an additional tag: @pytest.mark.long def…
JS.
  • 14,781
  • 13
  • 63
  • 75
59
votes
3 answers

parameterized test with cartesian product of arguments in pytest

Just wondering, is there any (more) elegant way of parameterizing with the cartesian product? This is what I figured out so far: numbers = [1,2,3,4,5] vowels = ['a','e','i','o','u'] consonants = ['x','y','z'] cartesian = [elem for elem in…
memecs
  • 7,196
  • 7
  • 34
  • 49
59
votes
8 answers

How to speed up pytest

Is there some way to speed up the repeated execution of pytest? It seems to spend a lot of time collecting tests, even if I specify which files to execute on the command line. I know it isn't a disk speed issue either since running pyflakes across…
edA-qa mort-ora-y
  • 30,295
  • 39
  • 137
  • 267
57
votes
4 answers

How to run all PyTest assertions even if some of them fail?

I am looking for a way to run all of the assertions in my unit tests in PyTest, even if some of them fail. I know there must be a simple way to do this. I checked the CLI options and looked through this site for similar questions/answers but didn't…
Jeff Wright
  • 1,014
  • 1
  • 11
  • 17
56
votes
3 answers

Testing class methods with pytest

In the documentation of pytest various examples for test cases are listed. Most of them show the test of functions. But I’m missing an example of how to test classes and class methods. Let’s say we have the following class in the module cool.py we…
laserbrain
  • 985
  • 3
  • 10
  • 20
55
votes
4 answers

Running pytest test functions inside a jupyter notebook

I'm working on a presentation about python testing options, and one of the technologies I want to demo is pytest. I'm planning to do the presentation from an jupyter/ipython notebook. Ideally I'd like to be able to define a test function in a cell…
abingham
  • 1,294
  • 1
  • 10
  • 17
54
votes
6 answers

Is there a way to control how pytest-xdist runs tests in parallel?

I have the following directory layout: runner.py lib/ tests/ testsuite1/ testsuite1.py testsuite2/ testsuite2.py testsuite3/ testsuite3.py testsuite4/ …
superselector
  • 3,007
  • 3
  • 19
  • 10
52
votes
3 answers

How do I force pytest to write color output?

How do I force pytest to show the results in color, even when writing to a pipe? There does not seem to be any command line option to do so.
JanKanis
  • 6,346
  • 5
  • 38
  • 42
51
votes
3 answers

“ indirect fixture” error using pytest. What is wrong?

def fatorial(n): if n <= 1: return 1 else: return n*fatorial(n - 1) import pytest @pytest.mark.parametrize("entrada","esperado",[ (0,1), (1,1), (2,2), (3,6), (4,24), (5,120) ]) def…
Laurinda Souza
  • 1,207
  • 4
  • 14
  • 29
51
votes
2 answers

How to mock requests using pytest?

I'm writing some unit test code and I would like to mock requests module that is being used inside my function: import requests def get_employee(id): resp = requests.get(f'{__BASE_URL}/employee/{id}') if resp.status_code == 404: …
user866364
50
votes
3 answers

parametrize and running a single test in pytest

How can I run a single test out of a set configured with parametrize? Let's say I have the following test method: @pytest.mark.parametrize(PARAMETERS_LIST, PARAMETERS_VALUES) def test_my_feature(self, param1, param2, param3): """ test doc …
Avishay Cohen
  • 1,978
  • 2
  • 21
  • 34
50
votes
3 answers

How do I make pytest fixtures work with decorated functions?

py.test seems to fail when I decorate test functions which has a fixture as an argument. def deco(func): @functools.wraps(func) def wrapper(*args, **kwargs): return func(*args, **kwargs) return wrapper @pytest.fixture def…
jck
  • 1,910
  • 4
  • 18
  • 25
48
votes
8 answers

What to do when a py.test hangs silently?

While using py.test, I have some tests that run fine with SQLite but hang silently when I switch to Postgresql. How would I go about debugging something like that? Is there a "verbose" mode I can run my tests in, or set a breakpoint ? More…
Hexatonic
  • 2,251
  • 2
  • 21
  • 26