Code Coverage and Ternary Operators

Consider that we test this function in module.py:

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

And we have the following test case in 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)

If we run tests with pytest"branch reach" enabled with HTML output reports:

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

The report will require 100% coverage of branches with all "partial" branches:

enter image description here

But we obviously did not cover the part else 1 / 0at all.

Is there a way to improve reporting to see the uncovered parts of ternary operators?

+4
source share

Source: https://habr.com/ru/post/1691294/


All Articles