By default, Pytest checks all files matching its template.
To enable testing one file at a time, particularly useful for Spyder or other IDE debugging, add at the bottom of each
if __name__ == '__main__': pytest.main(['-r', 'a', '-v', __file__])
Then the file can be interactively debugged in Spyder or other IDE without running all the other test files too.
For Pytest to find and execute test scripts:
- each function name must begin with
def test_my1(a, b):
- each test file name must begin with
- each test file should be under a subdirectory somewhere in the project named
To run self-tests on a specific file
tests/test_my.py from the command line:
If you already have a bunch of files with:
if __name__ == '__main__': pytest.main()
fix this by
sed -i 's/pytest.main()/pytest.main([__file__])/' test_*.py
- Pytest test function finding rules