Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing line in coverage, even when hit #577

Closed
mantomas opened this issue Jan 6, 2023 · 1 comment
Closed

Missing line in coverage, even when hit #577

mantomas opened this issue Jan 6, 2023 · 1 comment

Comments

@mantomas
Copy link

mantomas commented Jan 6, 2023

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
Copy link
Collaborator

nedbat commented Jan 6, 2023

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

@nedbat nedbat closed this as not planned Won't fix, can't repro, duplicate, stale Jan 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants