当覆盖范围启用分支机构覆盖时,如何计算覆盖率百分比。
我正在使用coverage.py工具来获取Python代码的覆盖范围。如果我在下面使用没有-Branch标志的命令,
coverage run test_cmd
覆盖范围报告,
Name Stmts Miss Cover
--------------------------------------------------------------------------------------------------------------------------
/path/file.py 9 2 78%
从中我知道,封面百分比值是这样得出
cover = (Stmts Covered/total stmts)*100 = (9-2/9)*100 = 77.77%
的
coverage run --branch test_cmd
我会得到这样的 这样的覆盖范围报告,
Name Stmts Miss Branch BrPart Cover
----------------------------------------------------------------------------------------------------------------------------------------
/path/file.py 9 2 2 1 73%
从本报告中,我无法理解用于获得封面的公式= 73%的数字
如何来了,这是代码覆盖的正确值吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
虽然您可能不必太担心确切的数字,但这是它们的计算方式:
从您给出的输出中读取,我们有:
然后进行计算(我稍微简化了语法,在实际代码中,所有内容都隐藏了 背后。
属性 nedbat/coveragepy/blob/a9d582a47b41068d2a08ecf6c46bbbb10218cb5eec/coverage/coverage/results.py#l205-l207
https://github.com/nedbat/coveragepy/blob/8a5049e0fe6b717396f2af14b38d675555551ba/ /nedbat/coveragepy/blob/8a5049e0fe6b717396f2af14b38d67555555555551ba/coverage/coverage/coverage/Results.py#l259-l263“ AF14B38D67555F9C51BA/覆盖范围/结果。Py#l259-- l263
现在将所有这些都放在脚本中,添加
print(pc_covered)
73%。
While you probably shouldn't worry too much about the exact numbers, here is how they are calculated:
Reading from the output you gave, we have:
Then it is calculated (I simplified the syntax a bit, in the real code everything is hidden behind properties.)
https://github.com/nedbat/coveragepy/blob/a9d582a47b41068d2a08ecf6c46bb10218cb5eec/coverage/results.py#L205-L207
https://github.com/nedbat/coveragepy/blob/8a5049e0fe6b717396f2af14b38d67555f9c51ba/coverage/results.py#L210-L212
https://github.com/nedbat/coveragepy/blob/8a5049e0fe6b717396f2af14b38d67555f9c51ba/coverage/results.py#L259-L263
https://github.com/nedbat/coveragepy/blob/master/coverage/results.py#L215-L222
Now put all of this in a script, add
print(pc_covered)
and run it:It is then rounded of course, so there you have your
73%
.