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

How to share object from fixture to all tests using pytest?

What is the best way to define an object in a fixture with session scope and autouse=True, so it will be available to all tests? @pytest.fixture(scope='session', autouse=True) def setup_func(request): obj = SomeObj() Next thing, I want some…
alnet
  • 1,093
  • 1
  • 12
  • 24
29
votes
5 answers

Use docstrings to list tests in py.test

Here is a simple test file: # test_single.py def test_addition(): "Two plus two is still four" assert 2 + 2 == 4 def test_addition2(): "One plus one is still two" assert 1 + 1 == 2 The default output in py.test is like $ py.test…
Matthias Berth
  • 763
  • 1
  • 5
  • 14
28
votes
3 answers

Why pytest always says " ImportError: attempted relative import with no known parent package"

I'm new to pytest and python. My project structure is : projroot/ |-src/ |-a.py |-test/ |-test_a.py and test_a.py is: from ..src import a def test_a(): pass Then run pytestunder projroot: projroot>pytest And always there is the error info:…
Jacques Yang
  • 481
  • 1
  • 4
  • 6
28
votes
3 answers

Python Pytest: show full variables values on error (no minimization)

I am using pytest 3.9 on Python 3.6 and did not find the answer to this question in pytest docs. I have some test cases which involves checking if a given key is in a dictionary. When that key is not there, pytest shows the failure, but it somehow…
Guillaume
  • 5,497
  • 3
  • 24
  • 42
28
votes
2 answers

Chaining tests and passing an object from one test to another

I'm trying to pass the result of one test to another in pytest - or more specifically, reuse an object created by the first test in the second test. This is how I currently do it. @pytest.fixture(scope="module") def result_holder: return…
pills
  • 656
  • 1
  • 5
  • 10
28
votes
1 answer

pytest assert message customization with variable introspection

In the pytest documentation it says that you can customize the output message when an assert fails. I want to customize the assert message when testing a REST API method it returns an invalid status code: def test_api_call(self, client): …
Marc Tudurí
  • 1,869
  • 3
  • 18
  • 22
28
votes
3 answers

Change pytest rootdir

I am stuck with this incredibly silly error. I am trying to run pytest on a Raspberry Pi using bluepy. pi@pi:~/bluepy/bluepy $ pytest test_asdf.py ============================= test session starts ============================== platform…
Gabriele
  • 737
  • 2
  • 8
  • 20
28
votes
5 answers

Is there a way to change the location of pytest's .cache directory?

I need to be able to change the location of pytest's .cache directory to the env variable, WORKSPACE. Due to server permissions out of my control, I am running into this error because my user does not have permission to write in the directory where…
nonbot
  • 873
  • 2
  • 9
  • 15
28
votes
3 answers

Default skip test unless command line parameter present in py.test

I have a long run test, which lasts 2 days, which I don't want to include in a usual test run. I also don't want to type command line parameters, that would deselect it and other tests at every usual test run. I would prefer to select a…
Roland Puntaier
  • 3,250
  • 30
  • 35
28
votes
1 answer

Parallely running parameterized tests in pytest

I wanted to run parameterized test functions in parallel. This is for a concurrency testing scenario. Same testcase runs in parallel with different parameters in a device. After completing all the parameterized variant of one test function, I want…
kanak
  • 533
  • 1
  • 5
  • 8
28
votes
4 answers

py.test: Temporary folder for the session scope

The tmpdir fixture in py.test uses the function scope and thus isn't available in a fixture with a broader scope such as session. However, this would be useful for some cases such as setting up a temporary PostgreSQL server (which of course…
ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
28
votes
4 answers

How to mock/set system date in pytest?

In some of my tests I am having a problem that they fail on Travis because of time and time zone problems, so I want to mock system time for my test. How can I do this?
adarsh
  • 6,738
  • 4
  • 30
  • 52
28
votes
2 answers

Making py.test, coverage and tox work together: __init__.py in tests folder?

I'm having a weird problem with tox, py.test, coverage and pytest-cov: when py.test with the --cov option is launched from tox, it seems to require an __init__.py file in the tests folder which is not immediately obvious. While writing this post, I…
aldanor
  • 3,371
  • 2
  • 26
  • 26
27
votes
4 answers

How to run py.test against different versions of python?

Is it possible to run py.test with different versions of python without plugins (like xdist) or tox?
Vladimir Keleshev
  • 13,753
  • 17
  • 64
  • 93
27
votes
3 answers

py.test gives Coverage.py warning: Module sample.py was never imported

I ran a sample code from this thread. How to properly use coverage.py in Python? However, when I executed this command py.test test.py --cov=sample.py it gave me a warning, therefore, no report was created. platform linux2 -- Python 2.7.12,…
pypabot
  • 271
  • 1
  • 4
  • 4