如何在 Python 中正确使用coverage.py?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36517137/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 17:58:36  来源:igfitidea点击:

How to properly use coverage.py in Python?

pythonunit-testingpython-3.xcoverage.py

提问by Groosha

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

我刚刚开始使用Coverage.py模块,所以决定做一个简单的测试来检查它是如何工作的。

Sample.py

示例.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.

如您所见,我的所有代码都包含测试,而 py.test 表示所有代码都通过了。我希望 Coverage.py 显示 100% 的覆盖率。嗯,不。

Coverage.py results

Coverage.py 结果

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

好吧,Coverage.py 可能看不到 test.py 文件,所以我将测试函数复制到sample.py文件并再次运行 Coverage:
在此处输入图片说明

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%:

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

enter image description here

在此处输入图片说明

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.

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

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.py 可能只是观察哪些代码行没有执行,所以我应该为这些行编写测试。但是有些行已经执行但没有被测试覆盖,所以 Coverage.py 在这里会失败。

采纳答案by fips

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:

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

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.pyand record/display coverage report on sample.py.

这意味着在 上运行测试模块test.py并记录/显示覆盖率报告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.pyand record (only) coverage on sample.py- don't display a report.

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

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

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

coverage report -m

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

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

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

Coverage supports more switches like --includeand --omitto 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

覆盖的支持能力切换等--include--omit包含/排除使用路径模式的文件。有关更多信息,请查看他们的文档:https: //coverage.readthedocs.io/en/coverage-4.5.1/cmd.html#reporting

回答by Ned Batchelder

It's a little hard to parse through your experiments, and you haven't included the command lines you used with each experiment. But: if you run the tests with:

解析你的实验有点困难,而且你没有包含你在每个实验中使用的命令行。但是:如果您使用以下命令运行测试:

python -m py.test test.py

then you can run them under coverage.py with:

然后你可以在coverage.py下运行它们:

coverage run -m py.test test.py