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

How to I display why some tests where skipped while using py.test?

I am using skipIf() from unittest for skipping tests in certain conditions. @unittest.skipIf(condition), "this is why I skipped them!") How do I tell py.test to display skipping conditions? I know that for unittest I need to enable the verbose…
sorin
  • 161,544
  • 178
  • 535
  • 806
68
votes
3 answers

KeyError in module 'threading' after a successful py.test run

I'm running a set of tests with py.test. They pass. Yippie! But I'm getting this message: Exception KeyError: KeyError(4427427920,) in
kkurian
  • 3,844
  • 3
  • 30
  • 49
68
votes
7 answers

@Patch decorator is not compatible with pytest fixture

I have encountered something mysterious, when using patch decorator from mock package integrated with pytest fixture. I have two modules: -----test folder -------func.py -------test_test.py in func.py: def a(): …
Hello lad
  • 17,344
  • 46
  • 127
  • 200
67
votes
3 answers

Pytest fixture for a class through self not as method argument

Often I'll write a test class that uses a pytest fixture in every method. Here's an example. I'd like to be able to avoid having to write the fixture name in the signature of every method. It's not DRY. How can this be done? I would like to be able…
Donal
  • 8,430
  • 3
  • 16
  • 21
67
votes
6 answers

How can you test that two dictionaries are equal with pytest in python

Trying to assert that two dictionaries that have nested contents are equal to each other (order doesn't matter) with pytest. What's the pythonic way to do this?
John Mike
  • 1,895
  • 3
  • 17
  • 26
67
votes
5 answers

Disable autouse fixtures on specific pytest marks

Is it possible to prevent the execution of "function scoped" fixtures with autouse=True on specific marks only? I have the following fixture set to autouse so that all outgoing requests are automatically mocked out: @pytest.fixture(autouse=True) def…
Mattew Whitt
  • 2,194
  • 1
  • 15
  • 19
67
votes
2 answers

Python project directory structure / pytest trouble

This should be the easiest problem on earth, but even after extensive searching and tinkering, I'm still in deep trouble with finding a "correct" way to lay a directory structure and manage to run pytest etc correctly. Let's say my I have a program…
Basti
  • 2,228
  • 1
  • 19
  • 25
65
votes
3 answers

how to get which statements are missed in python test coverage

I am new to python, I have written test cases for my class , I am using python -m pytest --cov=azuread_api to get code coverage. I am getting coverage on the console as How do I get which lines are missed by test for e.g in aadadapter.py…
Planet-Zoom
  • 1,005
  • 2
  • 9
  • 20
65
votes
10 answers

Coverage.py warning: No data was collected. (no-data-collected)

I am trying to find the coverage using coverage module for a django project but gets Coverage.py warning: No data was collected. (no-data-collected) My project folder has src and tests folders. When I run coverage run -m pytest && coverage…
1010101
  • 843
  • 1
  • 6
  • 11
65
votes
4 answers

List available tests with py.test

I can't find a way to list the tests which I can call with py.test -k PATTERN How can I see the list of the available tests?
guettli
  • 25,042
  • 81
  • 346
  • 663
65
votes
9 answers

How to monkeypatch python's datetime.datetime.now with py.test?

I need to test functions which uses datetime.datetime.now(). What is the easiest way to do this?
sashk
  • 3,946
  • 2
  • 33
  • 41
63
votes
2 answers

pytest cannot find module

I am following the pytest good practices or at least I think I am. However, pytest cannot find my module. It seems not to include the current directory in its PYTHONPATH. The source file: def add(x, y): return x + y The test file: import…
Sardathrion - against SE abuse
  • 17,269
  • 27
  • 101
  • 156
63
votes
6 answers

Pytest and Python 3

I've installed pytest 2.3.4 under Debian Linux. By default it runs under Python 2.7, but sometimes I'd like to run it under Python 3.x, which is also installed. I can't seem to find any instructions on how to do that. The PyPI Trove classifiers…
Joe Abbate
  • 1,692
  • 1
  • 13
  • 19
62
votes
3 answers

How can I limit the maximum running time for a unit test?

I am currently running some unit tests that might either take a long time before failing or run indefinitely. In a successful test run they will always complete within a certain amount of time. Is it possible to create a pytest unit test that will…
shuttle87
  • 15,466
  • 11
  • 77
  • 106
61
votes
4 answers

Can I pass arguments to pytest fixtures?

The baseline of all my tests is that there will always be a taxi with at least one passenger in it. I can easily achieve this setup with some basic fixtures: from blah import Passenger, Taxi @pytest.fixture def passenger(): return…
pyzzled
  • 611
  • 1
  • 5
  • 4