pythonunit-testingcoverage.pypython-coverage

Exclude unit tests from coverage in python


I am new to using coverage.py. I used coverage run unit_tests.py which ran my tests. Then I used coverage report which generated the following coverage summary:

Name         Stmts   Miss  Cover
--------------------------------
cardnames       28      0   100%
dominion       458    210    54%
unit_tests     181      0   100%
--------------------------------
TOTAL          667    210    69%

Apart from including cardnames.py and dominion.py which I am trying to test inside unit_tests.py, the coverage report also includes the unit_tests.py file itself. (in the coverage calculation). How can I exclude this file from the report?


Solution

  • From their documentation:

    You can further fine-tune coverage.py’s attention with the --include and --omit switches (or [run] include and [run] omit configuration values). --include is a list of filename patterns. If specified, only files matching those patterns will be measured. --omit is also a list of filename patterns, specifying files not to measure.

    So, scripting from the hip, the syntax would be something like coverage run --source=<files to be included> --omit=unit_tests.py unit_tests.py.