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

9.2k views Asked by At

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

There are 2 answers

0
Miguel Trejo On BEST ANSWER

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

0
H.T. Kong On

I assumed you enabled branch coverage. Basically, according to the post in the link, 3->5 just means the branching that jump from line 3 to line 5, which in your case, it's referring to when this if type(self) == type(other) is false and jump directly to return NotImplemented(which never happened in your test case).

credit to this question, How do I interpret Python coverage.py branch coverage results?