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
109
votes
3 answers

Grouping tests in pytest: Classes vs plain functions

I'm using pytest to test my app. pytest supports 2 approaches (that I'm aware of) of how to write tests: In classes: test_feature.py -> class TestFeature -> def test_feature_sanity In functions: test_feature.py -> def test_feature_sanity Is…
NI6
  • 2,477
  • 5
  • 17
  • 28
109
votes
9 answers

Create and import helper functions in tests without creating packages in test directory using py.test

Question How can I import helper functions in test files without creating packages in the test directory? Context I'd like to create a test helper function that I can import in several tests. Say, something like this: # In common_file.py def…
Juan Carlos Coto
  • 11,900
  • 22
  • 62
  • 102
105
votes
4 answers

How to run a method before all tests in all classes?

I'm writing selenium tests, with a set of classes, each class containing several tests. Each class currently opens and then closes Firefox, which has two consequences: super slow, opening firefox takes longer than running the test in a…
Hugh Perkins
  • 7,975
  • 7
  • 63
  • 71
102
votes
6 answers

Importing correctly with pytest

I just got set up to use pytest with Python 2.6. It has worked well so far with the exception of handling "import" statements: I can't seem to get pytest to respond to imports in the same way that my program does. My directory structure is as…
NcAdams
  • 2,521
  • 4
  • 21
  • 32
94
votes
25 answers

VSCode pytest test discovery fails

Pytest test discovery is failing. The UI states: Test discovery error, please check the configuration settings for the tests The output window states: Test Discovery failed: Error: Traceback (most recent call last): File…
devlife
  • 15,275
  • 27
  • 77
  • 131
94
votes
4 answers

pytest fixtures Redefining name from outer scope [pylint]

I'm learning pytest and I lint my code with pylint. But pylint still complaints about: W0621: Redefining name %r from outer scope (line %s) for the following example from pytest: # test_wallet.py @pytest.fixture def my_wallet(): '''Returns a…
oglop
  • 1,175
  • 1
  • 9
  • 15
94
votes
7 answers

Why PyTest is not collecting tests (collected 0 items)?

I have been trying to run unit tests using pytest. I wrote a module with one class and some methods inside that class, and I wrote a unit test for this module (with a simple assert statement to check equality of lists) where I first instantiated the…
Ramkumar Hariharan
  • 967
  • 1
  • 7
  • 8
93
votes
9 answers

Test case execution order in pytest

I am using pytest. I have two files in a directory. In one of the files there is a long running test case that generates some output. In the other file there is a test case that reads that output. How can I ensure the proper execution order of the…
Kocka
  • 1,634
  • 2
  • 13
  • 21
92
votes
5 answers

Testing logging output with pytest

I am trying to write a test, using pytest, that would check that a specific function is writing out a warning to the log when needed. For example: In module.py: import logging LOGGER = logging.getLogger(__name__) def run_function(): if…
lauren.marietta
  • 2,125
  • 1
  • 11
  • 19
92
votes
7 answers

How can I assert lists equality with pytest

I'm trying to make some unit tests with pytest. I was thinking about doing things like that: actual = b_manager.get_b(complete_set) assert actual is not None assert actual.columns == ['bl', 'direction', 'day'] The first assertion in ok but with the…
bAN
  • 13,375
  • 16
  • 60
  • 93
91
votes
3 answers

An error while trying to execute tests on python 3.10 with pytest

If I try to execute tests using pytest with python 3.10 I got the following error: TypeError: required field "lineno" missing from alias Google says it's a known problem, but I found no way to solve it. Tests start fine if I launch them with 3.9
chydik
  • 1,037
  • 1
  • 7
  • 5
91
votes
4 answers

py.test skips test class if constructor is defined

I have following unittest code running via py.test. Mere presence of the constructor make the entire class skip when running py.test -v -s collected 0 items / 1 skipped Can anyone please explain to me this behaviour of py.test? I am interested in…
Zdenek Maxa
  • 1,319
  • 1
  • 10
  • 12
90
votes
4 answers

Ensuring py.test includes the application directory in sys.path

I have a project directory structure as follows (which I think is pretty standard): my_project setup.py mypkg __init__.py foo.py tests functional test_f1.py unit test_u1.py I'm…
Paul Moore
  • 6,569
  • 6
  • 40
  • 47
89
votes
9 answers

Unable to debug in pycharm with pytest

I cannot debug in PyCharm using py.test. All the test suite is running ok in "Debug mode" but it doesn't stop on breakpoints. I also have py.test as the default test runner. Maybe this is not important, but debugging works correctly in my Django…
davyria
  • 1,343
  • 2
  • 13
  • 23
88
votes
17 answers

ModuleNotFoundError with pytest

I want my tests folder separate to my application code. My project structure is like so myproject/ myproject/ myproject.py moduleone.py tests/ myproject_test.py myproject.py from moduleone import ModuleOne class…
myol
  • 8,857
  • 19
  • 82
  • 143