我正在使用覆盖率插件 (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)
最佳答案
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
关于python - 在 pytest 覆盖率报告中, "->"对缺失行意味着什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64406942/