当覆盖范围启用分支机构覆盖时,如何计算覆盖率百分比。

发布于 2025-01-30 03:22:27 字数 1127 浏览 6 评论 0 原文

我正在使用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%的数字

如何来了,这是代码覆盖的正确值吗?

I am using coverage.py tool to get coverage of python code. If I use the command without --branch flag like below,

coverage run test_cmd

I get the coverage report like this,

Name                                                                              Stmts   Miss  Cover
--------------------------------------------------------------------------------------------------------------------------
/path/file.py                                                                      9      2    78%

From this I understand that the cover percentage value is derived as this

cover = (Stmts Covered/total stmts)*100 = (9-2/9)*100 = 77.77%

But when I run coverage with --branch flag enabled like this

coverage run --branch test_cmd

I get the coverage report like this,

Name                                                                           Stmts   Miss Branch BrPart  Cover
----------------------------------------------------------------------------------------------------------------------------------------
/path/file.py                                                                    9      2      2      1    73%

From this report I am not able to understand the formula used to get Cover=73%

How is this number coming and is this correct value for code coverage?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

安静 2025-02-06 03:22:27

虽然您可能不必太担心确切的数字,但这是它们的计算方式:

从您给出的输出中读取,我们有:

n_statements = 9
n_missing = 2
n_branches = 2
n_missing_branches = 1

然后进行计算(我稍微简化了语法,在实际代码中,所有内容都隐藏了 背后。

属性 nedbat/coveragepy/blob/a9d582a47b41068d2a08ecf6c46bbbb10218cb5eec/coverage/coverage/results.py#l205-l207

n_executed = n_statements - n_missing

n_executed_branches = n_branches - n_missing_branches

https://github.com/nedbat/coveragepy/blob/8a5049e0fe6b717396f2af14b38d675555551ba/ /nedbat/coveragepy/blob/8a5049e0fe6b717396f2af14b38d67555555555551ba/coverage/coverage/coverage/Results.py#l259-l263“ AF14B38D67555F9C51BA/覆盖范围/结果。Py#l259-- l263

numerator = n_executed + n_executed_branches
denominator = n_statements + n_branches

pc_covered = (100.0 * numerator) / denominator

现在将所有这些都放在脚本中,添加 print(pc_covered)

72.72727272727273

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:

n_statements = 9
n_missing = 2
n_branches = 2
n_missing_branches = 1

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

n_executed = n_statements - n_missing

https://github.com/nedbat/coveragepy/blob/8a5049e0fe6b717396f2af14b38d67555f9c51ba/coverage/results.py#L210-L212

n_executed_branches = n_branches - n_missing_branches

https://github.com/nedbat/coveragepy/blob/8a5049e0fe6b717396f2af14b38d67555f9c51ba/coverage/results.py#L259-L263

numerator = n_executed + n_executed_branches
denominator = n_statements + n_branches

https://github.com/nedbat/coveragepy/blob/master/coverage/results.py#L215-L222

pc_covered = (100.0 * numerator) / denominator

Now put all of this in a script, add print(pc_covered) and run it:

72.72727272727273

It is then rounded of course, so there you have your 73%.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文