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
180
votes
4 answers

How to use pytest to check that Error is NOT raised

Let's assume we have smth like that : import py, pytest ERROR1 = ' --- Error : value < 5! ---' ERROR2 = ' --- Error : value > 10! ---' class MyError(Exception): def __init__(self, m): self.m = m def __str__(self): return…
paraklet
  • 1,887
  • 2
  • 13
  • 6
179
votes
3 answers

How to test single file under pytest

How do you test a single file in pytest? I could only find ignore options and no "test this file only" option in the docs. Preferably this would work on the command line instead of setup.cfg, as I would like to run different file tests in the ide.…
simonzack
  • 19,729
  • 13
  • 73
  • 118
174
votes
7 answers

Run code before and after each test in py.test?

I want to run additional setup and teardown checks before and after each test in my test suite. I've looked at fixtures but not sure on whether they are the correct approach. I need to run the setup code prior to each test and I need to run the…
edA-qa mort-ora-y
  • 30,295
  • 39
  • 137
  • 267
161
votes
8 answers

How to tell py.test to skip certain directories?

I tried to use the norecursedirs option inside setup.cfg to tell py.test not to collect tests from certain directories but it seems it does ignore it. [tool:pytest] norecursedirs=lib/third When I run py.test I do see how it does get tests from…
sorin
  • 161,544
  • 178
  • 535
  • 806
137
votes
14 answers

How to pass environment variables to pytest

Before I start executing the tests in my Python project, I read some environment variables and set some variables with these values read. My tests will run on the desired environment based on these values read. For eg: Let's say the environment…
sridhar249
  • 4,471
  • 6
  • 26
  • 27
136
votes
8 answers

How to mock a readonly property with mock?

How do you mock a readonly property with mock? I tried: setattr(obj.__class__, 'property_to_be_mocked', mock.Mock()) but the issue is that it then applies to all instances of the class... which breaks my tests. Do you have any other idea? I don't…
charlax
  • 25,125
  • 19
  • 60
  • 71
135
votes
7 answers

pytest using fixtures as arguments in parametrize

I would like to use fixtures as arguments of pytest.mark.parametrize or something that would have the same results. For example: import pytest import my_package @pytest.fixture def dir1_fixture(): return '/dir1' @pytest.fixture def…
elveatles
  • 2,160
  • 3
  • 18
  • 16
133
votes
6 answers

py.test: error: unrecognized arguments: --cov=ner_brands --cov-report=term-missing --cov-config

when I am trying to run my test through command line py.test file_name.py I got this error: py.test: error: unrecognized arguments: --cov=ner_brands --cov-report=term-missing --cov-config How can I fix this?
Hello lad
  • 17,344
  • 46
  • 127
  • 200
129
votes
5 answers

How to execute ipdb.set_trace() at will while running pytest tests

I'm using pytest for my test suite. While catching bugs in complex inter-components test, I would like to place import ipdb; ipdb.set_trace() in the middle of my code to allow me to debug it. However, since pytest traps sys.stdin/sys.stdout ipdb…
manu
  • 3,544
  • 5
  • 28
  • 50
128
votes
2 answers

unittest Vs pytest

In unittest, I can setUp variables in a class and then the methods of this class can chose whichever variable it wants to use... class test_class(unittest.TestCase): def setUp(self): self.varA = 1 self.varB = 2 …
LuckyStarr
  • 1,465
  • 2
  • 12
  • 14
127
votes
10 answers

How to pass arguments in pytest by command line

I have a code and I need to pass the arguments like name from terminal. Here is my code and how to pass the arguments. I am getting a "File not found" kind error that I don't understand. I have tried the command in the terminal: pytest .py…
ashish sarkar
  • 1,289
  • 2
  • 9
  • 7
125
votes
14 answers

Py.test No module named *

I have a folder structure like this App --App --app.py --Docs --Tests --test_app.py In my test_app.py file, I have a line to import my app module. When I run py.test on the root folder, I get this error about no module named app. How…
zsljulius
  • 3,813
  • 8
  • 36
  • 61
113
votes
3 answers

long running py.test stop at first failure

I am using pytest, and the test execution is supposed to run until it encounters an exception. If the test never encounters an exception it should continue running for the rest of time or until I send it a SIGINT/SIGTERM. Is there a programmatic way…
Matt
  • 3,592
  • 5
  • 21
  • 26
112
votes
6 answers

Test if code is executed from within a py.test session

I'd like to connect to a different database if my code is running under py.test. Is there a function to call or an environment variable that I can test that will tell me if I'm running under a py.test session? What's the best way to handle this?
Laizer
  • 5,932
  • 7
  • 46
  • 73
111
votes
7 answers

How to parametrize a Pytest fixture

Consider the following Pytest: import pytest class TimeLine(object): instances = [0, 1, 2] @pytest.fixture def timeline(): return TimeLine() def test_timeline(timeline): for instance in timeline.instances: assert instance % 2…
Kurt Peek
  • 52,165
  • 91
  • 301
  • 526