python - 在 pytest 覆盖率报告中, "->"对缺失行意味着什么?

标签 python pytest pytest-cov

我正在使用覆盖率插件 (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/

相关文章:

python - 从另一个 pip 安装的包针对本地代码运行 py.test 测试

python-3.x - 测试一个函数是否被调用

python - 如何从用 numpy 的 tofile() 编写的原始二进制文件中读取 float

python - 最长公共(public)子序列的界

python - python有泛型方法吗?

python - 在 Travis 上阻止测试/进程的网络访问?

github - 带有工作服/github 操作的 Python 项目代码覆盖率徽章

python - 无法从requirements.txt安装pytest

python - python 中使用 zip 进行字符串格式化