代码覆盖和三元运算符

问题描述 投票:4回答:1

考虑我们在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输出报告的启用“分支覆盖”的pytest运行测试:

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

该报告将要求所有“部分”分支覆盖100%分支机构:

enter image description here

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

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

python testing code-coverage pytest coverage.py
1个回答
4
投票

分支覆盖只能测量从一行到另一行的分支,因为Python的跟踪工具目前仅支持每行跟踪。 Python 3.7引入了一些字节码级跟踪,但要使用它需要大量工作。

https://bitbucket.org/ned/coveragepy/issues/606/variable-assignment-with-an-if-else是一个封闭的问题。

© www.soinside.com 2019 - 2024. All rights reserved.