我正在更新一个测试覆盖率很差的继承存储库。repo 本身是一个 pytest 插件。我已经将 repo 更改为与tox一起使用pytest-cov,并将“原始”测试转换pytester为在测试插件时按照 pytest 文档中的建议使用。
测试和 tox 构建等效果很好。但是,覆盖率报告了类定义、导入等的错误未命中。这是因为代码本身是作为 pytest 实例化的一部分导入的,并且在测试实际开始之前不会被“覆盖”。
我已经阅读了 pytest 文档、pytest-cov 和覆盖率文档以及 tox 文档,并尝试了几种配置,但都无济于事。我已经用尽了可能会导致我找到一个好的解决方案的谷歌关键字组合池。
pkg_root/
.tox/
py3/
lib/
python3.7/
site-pacakges/
plugin_module/
supporting_module.py
plugin.py
some_data.dat
plugin_module/
supporting_module.py
plugin.py
some_data.dat
tests/
conftest.py
test_my_plugin.py
tox.ini
setup.py
Run Code Online (Sandbox Code Playgroud)
一些带有评论的相关片段:
[pytest]
addopts = --cov={envsitepackagesdir}/plugin_module --cov-report=html
testpaths = tests
Run Code Online (Sandbox Code Playgroud)
这个配置给我一个错误,没有收集数据;在这种情况下不会创建 htmlcov。
如果我只是使用--cov,我会得到(预期的)非常嘈杂的覆盖范围,它显示了功能命中和未命中,但是上面报告了导入、类定义等的错误未命中。
pytest_plugins = ['pytester'] # Entire contents of file!
Run Code Online (Sandbox Code Playgroud)
def test_a_thing(testdir):
testdir.makepyfile(
"""
def test_that_fixture(my_fixture):
assert my_fixture.foo == …Run Code Online (Sandbox Code Playgroud)