2

我正在使用覆盖插件(pytest --cov)运行 pytest,在报告中我得到以下行:

Name          Stmts   Miss Branch BrPart  Cover   Missing
---------------------------------------------------------
foo.py            5      1      2      1    71%   3->5, 5

我知道这3-5意味着它错过了第 3 到第 5 行,但我不知道是什么->意思。从测试逻辑来看,我希望只会5被报告。作为参考,这是我使用的代码:

# foo.py

class Test:
    def __lt__(self, other):
        if type(self) == type(other):
            return False
        return NotImplemented


# test_foo.py

def test_lt():
    test = Test()
    assert not (test < test)
4

2 回答 2

1

我假设您启用了分支覆盖。基本上,根据链接中的帖子,3-> 5 仅表示从第 3 行跳转到第 5 行的分支,在您的情况下,它指的是何时if type(self) == type(other)为假并直接跳转到return NotImplemented(在您的测试中从未发生过)案子)。

归功于这个问题,我如何解释 Python coverage.py 分支覆盖结果?

于 2020-10-17T20:36:22.870 回答
1

Coverage收集代码中从一行(源)到另一行(目标)的转换对。在某些情况下,可能会跳过某些转换,例如在条件语句或中断状态中,然后将其测量为缺少分支(或缺少转换)。

例如,在您的代码中有一个可以跳转的转换。

if type(self) == type(other):
       return False
   return NotImplemented

看到从第 3 行到第 5 行的转换不一定会发生,因为可能存在if语句不评估为False. 因此,由于缺少从第 3 行到第 5 行的跳转,分支覆盖会将此代码标记为未完全覆盖。

参考

分支覆盖如何工作。https://coverage.readthedocs.io/en/latest/branch.html#how-it-works

于 2020-10-17T20:53:44.257 回答