当我运行coverage.py命令时,报告中唯一的结果来自测试目录。显然,报告的值来自源文件目录中的代码覆盖率。我如何看待这个结果?
我在用:
coverage run -m unittest test.tests1.sometestfile1
以及从源目录执行和传递的所有导入/函数,但是该报告如下所示:
$ coverage report
Name Stmts Miss Cover
-----------------------------------------------
test/__init__.py 1 0 100%
test/../sometestfile1.py 116 69 41%
test/../sometestfile2.py 116 69 41%
test/../sometestfile3.py 116 69 41%
...
-----------------------------------------------
TOTAL 373 137 63%我尝试过将源dir添加到coverage的--source和--include选项中,但它并没有解决这个问题。
如何从实际源文件中查看覆盖率?
发布于 2022-03-24 11:03:57
解决方案是对pytest使用--source=选项。
您有:coverage run -m unittest test.tests1.sometestfile1,它只显示测试文件的覆盖范围。
您需要确保您的测试/Lib/site包中的文件得到了覆盖。(我假设您正在制作一个包,供用户导入,并可能供PyPi保存)。
这些都是由.tox/test子目录中的tox创建的。
这就是为什么coverage run -m --source=. unittest test.tests1.sometestfile1不能工作的原因。
您需要使用lib名称。
coverage run -m --source=<module> unittest test.tests1.sometestfile1
例如,对于我的项目pyweaving,它使用了这个目录结构:
pyweaving/
tests/
docs/
src/
pyweaving/
__init__.py
foo.py
data/
generators/
__init__.py
bar.py
setup.py (just the stub code)
setup.cfg
tox.ini我的覆盖率命令行如下所示:
coverage run -m --source=pyweaving pytest --basetemp="{envtmpdir}" {posargs}
https://stackoverflow.com/questions/67980243
复制相似问题