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
48
votes
2 answers

Does pytest parametrized test work with unittest class based tests?

I've been trying to add parametrized @pytest.mark.parametrize tests to a class based unittest. class SomethingTests(unittest.TestCase): @pytest.mark.parametrize(('one', 'two'), [ (1, 2), (2, 3)]) def test_default_values(self, one,…
graffic
  • 1,303
  • 2
  • 14
  • 18
48
votes
5 answers

Using py.test with coverage doesn't include imports

For Jedi we want to generate our test coverage. There is a related question in stackoverflow, but it didn't help. We're using py.test as a test runner. However, we are unable to add the imports and other "imported" stuff to the report. For example…
Dave Halter
  • 15,556
  • 13
  • 76
  • 103
47
votes
9 answers

How to share global variables between tests?

I have a global variable in conftest.py and use it in tests. For example: conftest.py api_version = 'v25' api_url = 'http://www.foobar.com/' + api_version test_foo.py from conftest import api_url import requests @pytest.fixture def data(): …
Andrey Glazkov
  • 2,398
  • 3
  • 17
  • 19
47
votes
2 answers

Creating a temporary directory in PyTest

My Python project imports pytest 2.9.0 fine without any issues. I want to create a new empty directory that will last only the life of the test session. I see that pytest offers temporary directory support: https://pytest.org/latest/tmpdir.html You…
Dai
  • 141,631
  • 28
  • 261
  • 374
47
votes
5 answers

Can I run line_profiler over a pytest test?

I have identified some long running pytest tests with py.test --durations=10 I would like to instrument one of those tests now with something like line_profiler or cprofile. I really want to get the profile data from the test itself as the pytest…
Atlas1j
  • 2,442
  • 3
  • 27
  • 35
47
votes
3 answers

How and where does py.test find fixtures

Where and how does py.test look for fixtures? I have the same code in 2 files in the same folder. When I delete conftest.py, cmdopt cannot be found running test_conf.py (also in same folder. Why is sonoftest.py not searched? #…
Dave
  • 4,184
  • 8
  • 40
  • 46
46
votes
7 answers

PytestUnknownMarkWarning: Unknown pytest.mark.xxx - is this a typo?

I have a file called test.py with the following code: import pytest @pytest.mark.webtest def test_http_request(): pass class TestClass: def test_method(self): pass pytest -s test.py passed but gave the following warnings: pytest…
Jonathan L
  • 9,552
  • 4
  • 49
  • 38
46
votes
3 answers

Async fixtures with pytest

How do I define async fixtures and use them in async tests? The following code, all in the same file, fails miserably. Is the fixture called plainly by the test runner and not awaited? @pytest.fixture async def create_x(api_client): x_id = await…
Pynchia
  • 10,996
  • 5
  • 34
  • 43
46
votes
1 answer

What does indirect = True/False in pytest.mark.parametrize do/mean?

I just want to understand what does it mean or what happens if I set indirect parameter to True or False in the pytest.mark.parametrize?
Froodo
  • 573
  • 1
  • 6
  • 16
45
votes
2 answers

How to get coverage reporting when testing a pytest plugin?

Context I am updating an inherited repository which has poor test coverage. The repo itself is a pytest plugin. I've changed the repo to use tox along with pytest-cov, and converted the "raw" tests to use pytester as suggested in the pytest…
Thomas Thorogood
  • 2,150
  • 3
  • 24
  • 30
45
votes
7 answers

import file mismatch in pytest

I've got a file in the package with 'test' in its name and when I run pytest I got an error import file mismatch: imported module 'my_project.my_file_test' has this __file__ attribute: …
vZ10
  • 2,468
  • 2
  • 23
  • 33
45
votes
2 answers

What's the meaning of the percentages displayed for each test on PyTest?

I'm new to testing with Pytest, and I've run into a minor but annoying hangup. In the command line test session results, I see my tests passing, but the percentage shown is not 100%, for some tests. With some aggressive logging, I was able to…
klreeher
  • 1,391
  • 2
  • 15
  • 27
45
votes
4 answers

Splitting a conftest.py file into several smaller conftest-like parts

I've got a large conftest.py file that I wish to split into smaller parts, for two reasons: The file is very large (~1000 lines, including documentation) Some of the fixtures depend on other fixtures, and I have no reason to expose those other…
alkalinity
  • 1,750
  • 2
  • 21
  • 32
45
votes
7 answers

Python: block network connections for testing purposes?

I'm trying to test a package that provides interfaces to a few web services. It has a test suite that is supposed to test most functions without connecting to the internet. However, there are some lingering tests that may attempt to connect to the…
keflavich
  • 18,278
  • 20
  • 86
  • 118
43
votes
5 answers

how to make py.test --cov skip virtualenv directory

Should I care how my tests cover the external libraries I'm using in my project ? The py.test --cov displays how all files are covered, including ones in my virtualenv directory. How can I make the output show only the coverage of the modules I've…
David Ben Ari
  • 2,259
  • 3
  • 21
  • 40