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
43
votes
4 answers

How to generate test report using pytest?

How can I generate test report using pytest? I searched for it but whatever I got was about coverage report. I tried with this command: py.test sanity_tests.py --cov=C:\Test\pytest --cov-report=xml But as parameters represents it generates coverage…
fhulprogrammer
  • 599
  • 2
  • 7
  • 16
42
votes
2 answers

difference between fixture and yield_fixture in pytest

I am going through pytest fixtures, and the following looks pretty similar, latest works pretty similar. Yes, the readability is better in yield_fixure, however could someone let me know what exactly is the difference. which should I use, in cases…
Gaurang Shah
  • 11,764
  • 9
  • 74
  • 137
42
votes
7 answers

Can I perform multiple assertions in pytest?

I'm using pytest for my selenium tests and wanted to know if it's possible to have multiple assertions in a single test? I call a function that compares multiple values and I want the test to report on all the values that don't match up. The problem…
ChrisG29
  • 1,501
  • 3
  • 25
  • 45
42
votes
2 answers

I want to use stdin in a pytest test

The PyTest documentation states that stdin is redirected to null as no-one will want to do interactive testing in a batch test context. This is true, but interactive is not the only use of stdin. I want to test code that uses stdin just as it would…
Russel Winder
  • 3,436
  • 1
  • 17
  • 12
42
votes
4 answers

How to skip a pytest using an external fixture?

Background I am running a py.test with a fixture in a conftest file. You can see the code below(this all works fine): example_test.py import pytest @pytest.fixture def platform(): return "ios" @pytest.mark.skipif("platform == 'ios'") def…
m3o
  • 3,881
  • 3
  • 35
  • 56
42
votes
2 answers

Writing a pytest function for checking the output on console (stdout)

This link gives a description how to use pytest for capturing console outputs. I tried on this following simple code, but I get error import sys import pytest def f(name): print "hello "+ name def test_add(capsys): f("Tom") …
brain storm
  • 30,124
  • 69
  • 225
  • 393
41
votes
3 answers

Disable Pytest in PyCharm

If I have a file beginning with "test_", PyCharm tries to run this with PyTest. I want to run it normally (as a regular Python script). How can I do this? Edit See Milo's answer below. If for some reason that does not work for you, as A. Romeau…
Peter
  • 12,274
  • 9
  • 71
  • 86
41
votes
5 answers

pytest run only the changed file?

I'm fairly new to Python, trying to learn the toolsets. I've figured out how to get py.test -f to watch my tests as I code. One thing I haven't been able to figure out is if there's a way to do a smarter watcher, that works like Ruby's Guard…
Andrew
  • 42,517
  • 51
  • 181
  • 281
40
votes
3 answers

pytest - Suppress DeprecationWarning from specific 3rd party modules

When I run pytest I'm getting some deprecation warnings from a 3rd party library. I'd like to be informed about any deprecation warnings in my own code, but not in a vendored copy of a library bundled with another 3rd-party library. This answer was…
WhiteHotLoveTiger
  • 2,088
  • 3
  • 30
  • 41
40
votes
4 answers

How to run tests without installing package?

I have some Python package and some tests. The files are layed out following http://pytest.org/latest/goodpractices.html#choosing-a-test-layout-import-rules Putting tests into an extra directory outside your actual application code, useful if you…
Colonel Panic
  • 132,665
  • 89
  • 401
  • 465
39
votes
4 answers

How to call setup once for all tests and teardown after all are finished

I have a bunch of tests written using pytest. There are all under a directory dir. For example: dir/test_base.py dir/test_something.py dir/test_something2.py ... The simplified version of code in them is as follows: test_base.py import…
vinodkone
  • 2,731
  • 4
  • 22
  • 21
39
votes
3 answers

pytest - specify log level from the CLI command running the tests

my team and I are using Pytest + Jenkins to automate our product testing. we have been using the standard Logging lib of python to get proper log messages during testing, before and after each test etc. we have multiple layers of logging, we log out…
Avishay Cohen
  • 1,978
  • 2
  • 21
  • 34
39
votes
3 answers

Pytest: run a function at the end of the tests

I would like to run a function at the end of all the tests. A kind of global teardown function. I found an example here and some clues here but it doesn't match my need. It runs the function at the beginning of the tests. I also saw the function…
user4780495
  • 2,642
  • 2
  • 18
  • 24
39
votes
2 answers

Multiple copies of a pytest fixture

Let's say I have a simple fixture like the following (using pytest-django, but it applies to pytest as well): @pytest.fixture def my_thing(request, db): thing = MyModel.objects.create() request.addfinalizer(lambda: thing.delete()) return…
Frank T
  • 8,268
  • 8
  • 50
  • 67
38
votes
5 answers

ModuleNotFoundError: No module named 'pytest'

After installing the pytest module in a virtual environment, I used the python code to call and run the prompt to find the pytest module. I installed the pytest module outside the virtual environment. I can call it normally with python. import…
御弟哥哥
  • 481
  • 1
  • 4
  • 3