如何在 Python 中正确使用coverage.py? [英] How to properly use coverage.py in Python?

查看:31
本文介绍了如何在 Python 中正确使用coverage.py?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始使用

好吧,Coverage.py 可能看不到 test.py 文件,所以我将测试函数复制到 sample.py 文件并再次运行 Coverage:

然后我添加了这段代码:

if __name__ == "__main__":打印(总和(2、4))打印(sum_only_positive(2, 4))打印(sum_only_positive(-1, 3))

并删除所有测试功能.之后,Coverage.py 显示为 100%:

为什么会这样?Coverage.py 不应该显示代码测试覆盖率,而不仅仅是执行覆盖率吗?我已经阅读了 Coverage.py 的官方FAQ,但找不到解决方案.
由于很多SO用户都熟悉代码测试和代码覆盖率,希望你能告诉我,我错在哪里了.

我在这里只有一个想法:Coverage.py 可能只是观察哪些代码行没有被执行,所以我应该为这些行编写测试.但是有些行已经执行但没有被测试覆盖,所以 Coverage.py 在这里会失败.

解决方案

Coverage 会查找 .coverage 文件来为您读取和生成该报告.Py.test 本身不会创建一个.你需要 py.test 插件来覆盖:

pip install pytest-cov

如果你已经有了它,那么你可以像这样同时运行:

py.test test.py --cov=sample.py

这意味着运行测试模块 test.py 并在 sample.py 上记录/显示覆盖率报告.

如果您需要多次测试运行并累积它们记录的覆盖率,然后显示最终报告,您可以像这样运行:

py.test test.py --cov=sample.py --cov-report=py.test test.py --cov=sample2.py --cov-report=py.test test.py --cov=sample3.py --cov-report=

这意味着运行测试模块 test.py 并记录(仅)sample.py 的覆盖率 - 不显示报告.

现在您可以单独运行coverage命令以获得完整的报告:

覆盖率报告 -m

上面的命令只是根据之前测试运行中累积的 .coverage 数据文件显示格式化的覆盖率报告.-m 表示显示丢失的行,即测试未覆盖的行:

Name Stmts Miss Cover Missing-----------------------------------------样本.py 6 0 100%

Coverage 支持更多开关,如 --include--omit 以使用路径模式包含/排除文件.有关更多信息,请查看他们的文档:https://coverage.readthedocs.io/en/coverage-4.5.1/cmd.html#reporting

I've just started using Coverage.py module and so decided to make a simple test to check how it works.

Sample.py

def sum(num1, num2):
    return num1 + num2


def sum_only_positive(num1, num2):
    if num1 > 0 and num2 > 0:
        return num1 + num2
    else:
        return None

test.py

from sample import sum, sum_only_positive

def test_sum():
    assert sum(5, 5) == 10

def test_sum_positive_ok():
    assert sum_only_positive(2, 2) == 4

def test_sum_positive_fail():
    assert sum_only_positive(-1, 2) is None

As you see, all my code is covered with tests and py.test says all of them pass. I expect Coverage.py to show 100% coverage. Well, no.

Well, Coverage.py may not see test.py file, so I copied test functions to sample.py file and ran Coverage again:

Then I added this block of code:

if __name__ == "__main__":
    print(sum(2, 4))
    print(sum_only_positive(2, 4))
    print(sum_only_positive(-1, 3))

and removed all test functions. After that, Coverage.py shows 100%:

Why is it so? Shouldn't Coverage.py show code test coverage, not just execution coverage? I've read an official F.A.Q. for Coverage.py, but can't find the solution.
Since many SO users are familiar with code testing and code coverage, I hope you can tell me, where am I mistaken.

I have just one thought here: Coverage.py may simply watch which lines of code aren't executed so I should write tests for those lines. But there're lines which are executed already but aren't covered with tests so Coverage.py will fail here.

解决方案

Coverage looks for a .coverage file to read and generate that report for you. Py.test on its own does not create one. You need py.test plugin for coverage:

pip install pytest-cov

If you already have it, then you can run both at once like this:

py.test test.py --cov=sample.py

Which means run test module test.py and record/display coverage report on sample.py.

If you need to have multiple test runs and accumulate their recorded coverage and then display a final report, you can run it like this:

py.test test.py --cov=sample.py --cov-report=
py.test test.py --cov=sample2.py --cov-report=
py.test test.py --cov=sample3.py --cov-report=

Which means run test module test.py and record (only) coverage on sample.py - don't display a report.

Now you can run coverage command separately for a complete report:

coverage report -m

The command above simply displays a formatted coverage report based on the accumulated .coverage data file from previous test runs. -m means show lines missed i.e. lines not covered by tests:

Name        Stmts   Miss  Cover   Missing
-----------------------------------------
sample.py       6      0   100%  

Coverage supports more switches like --include and --omit to include/exclude files using path patterns. For more info check out their docs: https://coverage.readthedocs.io/en/coverage-4.5.1/cmd.html#reporting

这篇关于如何在 Python 中正确使用coverage.py?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆