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)
Summary
In
for
loop, when there is a condition ending withFalse
(directly or evaluated from function) and then there is justcontinue
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 whencontinue
directly behind:
(not separate line).Reproducer
Run
pytest --cov --cov-report term-missing --cov-fail-under 100
with code snippet bellow.CLI output
Code