Skip to main content
 首页 » 编程设计

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

2025年05月04日9findumars

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

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.py 和 dominion.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 .