Make pytest check only one file

By default, Pytest checks all files matching its template. Pytest can select matching patterns of test files and test functions with the pytest -k ... option. Set breakpoint in a file to interactively debug in Spyder or other IDE.

Put default Pytest options in project “pyproject.toml”:

[tool.pytest.ini_options]
addopts = "-ra -v"

For Pytest to find and execute test scripts:

  • each function name must begin with test_ e.g. def test_my1(a, b):
  • each test file name must begin with test_ e.g. test_io.py
  • each test file should be under a subdirectory somewhere in the project named tests/

To run self-tests on a specific file from the command line:

pytest src/mypkg/tests/test_my.py

NOTE: Generally using __file__ is not robust for packages. Consider using conftest.py fixtures and / or tmp_path where filesystem interaction is required from Pytest.