Search code examples
pythonpytestpytest-cov

In a pytest coverage report, what does "->" mean for missing lines?


I'm running pytest with the coverage plugin (pytest --cov) and in the report I got the following line:

Name          Stmts   Miss Branch BrPart  Cover   Missing
---------------------------------------------------------
foo.py            5      1      2      1    71%   3->5, 5

I know that 3-5 would mean it missed lines 3 to 5, but I don't know what -> means. From the test logic, I'd expect only 5 to be reported. For reference, this is the code I used:

# 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)

Solution

  • Coverage collects pairs of transition in your code that goes from one line (the source) to another (the destination). In some cases, some transitions could be jumped, like in conditional statements or a break statememt, then it would be measured as a missing branch (or missing transition).

    For example, in your code there is one transition that can be jumped.

    if type(self) == type(other):
           return False
       return NotImplemented
    

    see that from line 3 to line 5 is a transition that can not necessarily happen as there could be the case that the if statement don't evaluate to False. Thus, branch coverage will flag this code as not fully covered because of the missing jump from line 3 to line 5.

    References

    How does branch coverage works. https://coverage.readthedocs.io/en/latest/branch.html#how-it-works