Fix Python – How to test single file under pytest

Question

Asked By – simonzack

How do you test a single file in pytest? I could only find ignore options and no “test this file only” option in the docs.

Preferably this would work on the command line instead of setup.cfg, as I would like to run different file tests in the ide. The entire suite takes too long.

Now we will see solution for issue: How to test single file under pytest


Answer

simply run pytest with the path to the file

something like

pytest tests/test_file.py

Use the :: syntax to run a specific test in the test file:

pytest test_mod.py::test_func

Here test_func can be a test method or a class (e.g.: pytest test_mod.py::TestClass).

For more ways and details, see “Specifying which tests to run” in the docs.

This question is answered By – Bryant

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0