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
824
votes
17 answers

How to see normal print output created during pytest run?

Sometimes I want to just insert some print statements in my code, and see what gets printed out when I exercise it. My usual way to "exercise" it is with existing pytest tests. But when I run these, I don't seem able to see any standard output (at…
Des
  • 9,195
  • 4
  • 18
  • 21
750
votes
14 answers

How do I properly assert that an exception gets raised in pytest?

Code: # coding=utf-8 import pytest def whatever(): return 9/0 def test_whatever(): try: whatever() except ZeroDivisionError as exc: pytest.fail(exc, pytrace=True) Output: ================================ test session…
Gill Bates
  • 14,330
  • 23
  • 70
  • 138
666
votes
11 answers

Specify which pytest tests to run from a file

How do I select which pytest tests to run from a file? For example, a file foo.txt containing a list of tests to be executed: tests_directory/foo.py::test_001 tests_directory/bar.py::test_some_other_test Similarly, Is there a way to select…
Sharad
  • 9,282
  • 3
  • 19
  • 36
538
votes
6 answers

What is conftest.py for in Pytest?

I'm trying to understand what conftest.py files are meant to be used for. In my (currently small) test suite I have one conftest.py file at the project root. I use it to define the fixtures that I inject into my tests. I have two questions: Is this…
Aviv Cohn
  • 15,543
  • 25
  • 68
  • 131
509
votes
12 answers

How do I print to console in pytest?

pytest will not print to the console when I use print. The documentation seems to say that it should work by default. I am using pytest my_tests.py to run this test: import myapplication as tum class TestBlogger: @classmethod def…
BBedit
  • 7,037
  • 7
  • 37
  • 50
397
votes
28 answers

PATH issue with pytest 'ImportError: No module named ...'

I used easy_install to install pytest on a Mac and started writing tests for a project with a file structure likes so: repo/ |--app.py |--settings.py |--models.py |--tests/ |--test_app.py Run py.test while in the repo…
MattoTodd
  • 14,467
  • 16
  • 59
  • 76
302
votes
8 answers

pytest: assert almost equal

How to do assert almost equal with pytest for floats without resorting to something like: assert x - 0.00001 <= y <= x + 0.00001 More specifically it will be useful to know a neat solution for quickly comparing pairs of float, without unpacking…
Vladimir Keleshev
  • 13,753
  • 17
  • 64
  • 93
295
votes
45 answers

pytest cannot import module while python can

I am working on a package in Python. I use virtualenv. I set the path to the root of the module in a .pth path in my virtualenv, so that I can import modules of the package while developing the code and do testing (Question 1: is it a good way to…
Zorglub29
  • 6,979
  • 6
  • 20
  • 37
241
votes
10 answers

Pass a parameter to a fixture function

I am using py.test to test some DLL code wrapped in a python class MyTester. For validating purpose I need to log some test data during the tests and do more processing afterwards. As I have many test_... files I want to reuse the tester object…
maggie
  • 3,935
  • 3
  • 27
  • 31
238
votes
7 answers

Logging within pytest tests

I would like to put some logging statements within test function to examine some state variables. I have the following code snippet: import pytest,os import logging logging.basicConfig(level=logging.DEBUG) mylogger =…
superselector
  • 3,007
  • 3
  • 19
  • 10
237
votes
8 answers

How do I correctly setup and teardown for my pytest class with tests?

I am using selenium for end to end testing and I can't get how to use setup_class and teardown_class methods. I need to set up browser in setup_class method, then perform a bunch of tests defined as class methods and finally quit browser in…
avasin
  • 9,186
  • 18
  • 80
  • 127
221
votes
9 answers

How do I disable a test using pytest?

Let's say I have a bunch of tests: def test_func_one(): ... def test_func_two(): ... def test_func_three(): ... Is there a decorator or something similar that I could add to the functions to prevent pytest from running just that test?…
ericmjl
  • 13,541
  • 12
  • 51
  • 80
218
votes
2 answers

Printing test execution times and pinning down slow tests with py.test

I am running unit tests on a CI server using py.test. Tests use external resources fetched over network. Sometimes test runner takes too long, causing test runner to be aborted. I cannot repeat the issues locally. Is there a way to make py.test…
Mikko Ohtamaa
  • 82,057
  • 50
  • 264
  • 435
202
votes
10 answers

How to suppress py.test internal deprecation warnings

Is there a way to suppress the pytest's internal deprecation warnings? Context: I'm looking to evaluate the difficulty of porting a test suite from nose to pytest. The suite is fairly large and heavily uses nose-style yield based test…
ev-br
  • 24,968
  • 9
  • 65
  • 78
198
votes
15 answers

How do I configure PyCharm to run py.test tests?

I want to start writing unit tests for my Python code, and the py.test framework sounds like a better bet than Python's bundled unittest. So I added a "tests" directory to my project, and added test_sample.py to it. Now I want to configure PyCharm…
Joe White
  • 94,807
  • 60
  • 220
  • 330
1
2 3
99 100