Coverage sometimes report missing branches for excluded lines · Issue #1271 · nedbat/coveragepy (original) (raw)

A minimal reproducible example:

$ cat pyproject.toml 
[tool.coverage.report]
exclude_lines = ['raise NotImplementedError']
$ cat -n test_a.py
     1  def foo(x):
     2      if x == 1:
     3          raise NotImplementedError
     4      return x
     5  
     6  def bar(x, y):
     7      if all(_ == __ for _, __ in zip(x, y)):
     8          raise NotImplementedError
     9      return x
    10  
    11  
    12  def test_0():
    13      foo(0)
    14      bar([0], [1])
$ pytest -q --cov test_a test_a.py --cov-branch --cov-report=term-missing 
.                                                                                                                                                      [100%]

----------- coverage: platform linux, python 3.9.2-final-0 -----------
Name        Stmts   Miss Branch BrPart  Cover   Missing
-------------------------------------------------------
test_a.py       9      0      2      2    82%   7->8, 7->exit
-------------------------------------------------------
TOTAL           9      0      2      2    82%

1 passed in 0.06s
$ pip list |egrep 'pytest|coverage'
coverage                      6.0.2
coverage-enable-subprocess    1.0
pytest                        6.2.5
pytest-cov                    3.0.0
pytest-doctestplus            0.9.0
pytest-forked                 1.3.0
pytest-timeout                2.0.1
pytest-xdist                  2.4.0

It seems for me, that the branch condition on the line 7 should be ignored as well as on the line 2.

See also this example, notice "pragma" statement to workaround the problem.