使用覆盖范围的 py.test 不包括导入 [英] Using py.test with coverage doesn't include imports
问题描述
对于 Jedi 我们想要生成我们的 测试覆盖率.stackoverflow 中有一个相关问题,但没有帮助.
For Jedi we want to generate our test coverage. There is a related question in stackoverflow, but it didn't help.
我们使用 py.test 作为测试运行器.但是,我们无法将导入和其他导入"的内容添加到报告中.例如 __init__.py
总是被报告为未发现:
We're using py.test as a test runner. However, we are unable to add the imports and other "imported" stuff to the report. For example __init__.py
is always reported as being uncovered:
Name Stmts Miss Cover
--------------------------------------------------
jedi/__init__ 5 5 0%
[..]
显然该文件正在被导入,因此应报告为已测试.
Clearly this file is being imported and should therefore be reported as tested.
我们像这样开始测试[*]:
We start tests like this [*]:
py.test --cov jedi
如您所见,我们正在使用 pytest-coverage
.
As you can see we're using pytest-coverage
.
那么如何正确计算像 __init__.py
这样的文件的覆盖率?
So how is it possible to properly count coverage of files like __init__.py
?
[*] 我们还尝试在没有 --doctest-modules
(从 pytest.ini
中删除)的情况下开始测试并通过 py 提前激活覆盖模块.test -p pytest_cov --cov jedi
.它们都不起作用.
[*] We also tried starting test without --doctest-modules
(removed from pytest.ini
) and activate the coverage module earlier by py.test -p pytest_cov --cov jedi
. Neither of them work.
我提供了赏金.请尝试在绝地中修复它.它是公开的.
I've offered a bounty. Please try to fix it within Jedi. It's publicly available.
推荐答案
@hynekcer 给了我正确的想法.但基本上最简单的解决方案在于其他地方:
@hynekcer gave me the right idea. But basically the easiest solution lies somewhere else:
摆脱pytest-cov
!
使用
coverage run --source jedi -m py.test
coverage report
相反!!!通过这种方式,您只是在当前的 py.test 配置上运行覆盖,这非常好!从哲学上讲,这也是正确的方法:让每个程序做好一件事——py.test
运行测试,coverage
检查代码覆盖率.
instead!!! This way you're just running a coverage on your current py.test configuration, which works perfectly fine! It's also philosophically the right way to go: Make each program do one thing well - py.test
runs tests and coverage
checks the code coverage.
现在这听起来像是在咆哮,但确实如此.pytest-cov
有一段时间不能正常工作了.一些测试失败了,只是因为我们使用了它.
Now this might sound like a rant, but really. pytest-cov
hasn't been working properly for a while now. Some tests were failing, just because we used it.
截至 2014,pytest-cov 似乎已易手.py.test --cov jedi test
似乎又是一个有用的命令(看评论).但是,您不需要使用它.但与 xdist
结合使用,它可以加快您的覆盖率报告.
As of 2014, pytest-cov seems to have changed hands. py.test --cov jedi test
seems to be a useful command again (look at the comments). However, you don't need to use it. But in combination with xdist
it can speed up your coverage reports.
这篇关于使用覆盖范围的 py.test 不包括导入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!