首页 文章

代码覆盖和三元运算符

提问于
浏览
4

考虑我们在 module.py 中测试了这个功能:

def f(a, b):
    return (a - b) if a > b else 1 / 0

并且,我们在 test_module.py 中有以下测试用例:

from unittest import TestCase

from module import f


class ModuleTestCase(TestCase):
    def test_a_greater_than_b(self):
        self.assertEqual(f(10, 5), 5)

如果我们使用带有HTML输出报告的启用"branch coverage"的 pytest 运行测试:

pytest test_module.py --cov=. --cov-branch --cov-report html

该报告将覆盖所有"partial"分支机构的100%分支机构覆盖率:

enter image description here

但是,我们显然根本没有涵盖 else 1 / 0 部分 .

有没有办法改进报告,以查看三元运算符的未覆盖部分?

1 回答

相关问题