pytest-dev / pytest-cov

Coverage plugin for pytest.
MIT License
1.76k stars 212 forks source link

Missing line in coverage, even when hit #577

Closed mantomas closed 1 year ago

mantomas commented 1 year ago

Summary

In for loop, when there is a condition ending with False (directly or evaluated from function) and then there is just continue in the indented block, this line is not recognized as hit. In the example below, if you uncomment the print function, coverage hit 100%. Tests pas with 100% also when continue directly behind : (not separate line).

Reproducer

Run pytest --cov --cov-report term-missing --cov-fail-under 100 with code snippet bellow.

CLI output

pytest --cov --cov-report term-missing  --cov-fail-under 100
=========================================================================================================================== test session starts ============================================================================================================================
platform linux -- Python 3.8.16, pytest-7.2.0, pluggy-1.0.0
rootdir: /home/tomasman/work/coverage_test
plugins: anyio-3.6.2, Faker-15.3.4, requests-mock-1.9.3, lazy-fixture-0.6.3, asyncio-0.16.0, cov-4.0.0
collected 1 item                                                                                                                                                                                                                                                           

test_foo.py .                                                                                                                                                                                                                                                        [100%]

---------- coverage: platform linux, python 3.8.16-final-0 -----------
Name          Stmts   Miss  Cover   Missing
-------------------------------------------
foo.py            5      1    80%   5
test_foo.py       8      0   100%
-------------------------------------------
TOTAL            13      1    92%

FAIL Required test coverage of 100% not reached. Total coverage: 92.31%

Code

# code in file foo.py
def foo(A, B):
    for i in A:
        if i in B or False:
            # print("hit")
            continue
    return True
# code in file test_foo.py
from foo import foo

def test_foo():
    # hit 'continue' 4 times
    A = [1, 2, 3, 4]
    B = [1, 2, 3, 4]
    assert foo(A, B)
    # never hit continue
    A = [1, 2, 3, 4]
    B = [5]
    assert foo(A, B)
nedbat commented 1 year ago

This is due to Python optimizing away jumps to jumps. Newer Pythons will trace it properly. Here's the coverage.py issue: https://github.com/nedbat/coveragepy/issues/198