Search code examples
pythonunit-testingpytestcoverage.pypytest-cov

Using pytest coverage, is there a way to tell which tests invoked a particular statement?


I am looking at an html coverage report generated by pytest-cov and I wonder if I can find which tests have invoked a particular line (which test covered a particular line).


Solution

  • Coverage.py includes a feature called "contexts" which can easily be used to annotate results with test names: https://coverage.readthedocs.io/en/latest/contexts.html

    Put this in your .coveragerc file:

    [run]
    dynamic_context = test_function