是否可以在 python 覆盖率报告中包含根本未被触及的文件

is it possible to include files in the python coverage report that weren't touched at all

我希望我的 python 覆盖率报告包含测试根本不会导入的文件。

所以如果我有一个包含 3 个文件的目录:test.py module1.py module2.py

并且两个模块中都有 print(__file__),test.py 中有 import module1

然后我 运行 这样的测试:

$ python3 -m venv venv
$ venv/bin/pip install coverage
$ venv/bin/coverage run test.py
......../module1.py
$ venv/bin/coverage report
Name         Stmts   Miss  Cover
--------------------------------
module1.py       1      0   100%
test.py          1      0   100%
--------------------------------
TOTAL            2      0   100%

但真正有用的是提醒我完全忘记了一个文件,请参阅列表中的 module2.py,旁边有 0%。

这可能吗?即使我明确包含该文件,它也不在报告中。

来自https://coverage.readthedocs.io/en/6.0.2/source.html

You can specify source to measure with the --source command-line switch, or the [run] source configuration value. The value is a comma- or newline-separated list of directories or importable names (packages or modules).

Specifying the source option also enables coverage.py to report on unexecuted files, since it can search the source tree for files that haven’t been measured at all.

使用相同的文件:

$ coverage run  test.py 
............../module1.py
$ coverage report
Name         Stmts   Miss  Cover
--------------------------------
module1.py       1      0   100%
test.py          1      0   100%
--------------------------------
TOTAL            2      0   100%
$ coverage run --source . test.py 
.............../module1.py
$ coverage report
Name         Stmts   Miss  Cover
--------------------------------
module1.py       1      0   100%
module2.py       1      1     0%
test.py          1      0   100%
--------------------------------
TOTAL            3      1    67%

第二个运行包括当前目录.中的所有文件。