覆盖率如何计算其百分比?

How does coverage calculate its percentages?

我从 运行 覆盖率中得到了这个结果,但我终究无法弄清楚覆盖率百分比是如何计算的..?

在此 example 中,它解释了分支覆盖范围,但没有说明示例的覆盖百分比。

更新: 以下是 pfind.py 的详细信息:

coverage 将每个分支计为两条可能的指令,并赋予它们与非分支指令相同的权重。使用这个公式:

(run+partial)/(statements+branches)

从代码看results.py,覆盖率计算在pc_covered, with the data obtained from ratio_covered函数中:

@property
def ratio_covered(self):
    """Return a numerator and denominator for the coverage ratio."""
    numerator = self.n_executed + self.n_executed_branches
    denominator = self.n_statements + self.n_branches
    return numerator, denominator

如您所见,如果启用分支覆盖,每个分支将被计算两次,一次作为语句,一次作为分支。