Python: Which tests cover this line?
Jan 19, 2023
When running a very large test suite, sometimes it’s useful to be able to determine which tests cover the line of code you’re about to change.
Especially if you don’t want to run the whole suite on your change.
Since version 5, python’s coverage module makes this fun and easy.
First, add these two lines to your .coveragerc file:
[run]
dynamic_context = test_function
Then, run your test suite:
pytest --cov notanorm --db sqlite --db mysql
Next, run your coverage report with the --show-contexts option:
coverage report --show-contexts
The resulting report will have a list of “contexts” applied to each covered line.
Each context is a test case that covers that at line.