从 python 的覆盖范围中排除单元测试

Exclude unit tests from coverage in python

我刚开始使用 coverage.py。我使用了 coverage run unit_tests.py 其中 运行 我的测试。然后我使用 coverage report 生成了以下覆盖率摘要:

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

除了包括我试图在 unit_tests.py 中测试的 cardnames.pydominion.py 之外,覆盖率报告还包括 unit_tests.py 文件本身。 (在覆盖率计算中)。如何从报告中排除此文件?

来自他们的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.

因此,从臀部编写脚本,语法类似于 coverage run --source=<files to be included> --omit=unit_tests.py unit_tests.py