Skip to content

Branch coverage not counted on "if" statement immediately followed by break or continue #1025

Description

@amykyta3

Describe the bug
It looks like branch coverage checks fail to detect an if statement branch if it is immediately followed by a continue or break statement.

See screenshots from the HTML report: (scroll down for copy/pastable code)
image

Inserting a no-op statement after the if block will cause coverage to be detected correctly:
image

To Reproduce
How can we reproduce the problem? Please be specific. Don't just link to a failing CI job. Answer the questions below:

  1. What version of Python are you using?: 3.8.2
  2. What version of coverage.py are you using? The output of coverage debug sys is helpful.: 5.2.1
  3. What versions of what packages do you have installed? The output of pip freeze is helpful.:
appdirs==1.4.3
apturl==0.5.2
asn1crypto==0.24.0
attrs==19.3.0
bcrypt==3.1.7
beautifulsoup4==4.8.2
blinker==1.4
Brlapi==0.7.0
certifi==2019.11.28
chardet==3.0.4
chrome-gnome-shell==0.0.0
Click==7.0
colorama==0.4.3
command-not-found==0.3
coverage==5.2.1
cryptography==2.8
cupshelpers==1.0
cycler==0.10.0
dbus-python==1.2.16
decorator==4.4.2
defer==1.0.6
distlib==0.3.0
distro==1.4.0
distro-info===0.23ubuntu1
duplicity==0.8.12.0
entrypoints==0.3
fasteners==0.14.1
filelock==3.0.12
future==0.18.2
GDAL==3.0.4
gpg===1.13.1-unknown
html5lib==1.0.1
httplib2==0.14.0
idna==2.8
importlib-metadata==1.5.0
iotop==0.6
ipython-genutils==0.2.0
Jinja2==2.10.1
jsonschema==3.2.0
jupyter-core==4.6.3
keyring==18.0.1
kiwisolver==1.0.1
language-selector==0.1
launchpadlib==1.10.13
lazr.restfulclient==0.14.2
lazr.uri==1.0.3
lockfile==0.12.2
louis==3.12.0
lxml==4.5.0
macaroonbakery==1.3.1
Mako==1.1.0
MarkupSafe==1.1.0
matplotlib==3.1.2
meld==3.20.2
monotonic==1.5
more-itertools==4.2.0
nbformat==5.0.4
netifaces==0.10.4
numpy==1.17.4
oauth==1.0.1
oauthlib==3.1.0
olefile==0.46
OWSLib==0.19.1
paramiko==2.6.0
pdfarranger==1.4.2
pexpect==4.6.0
pikepdf==1.10.3+dfsg
Pillow==7.0.0
Pivy==0.6.5
plotly==4.4.1
protobuf==3.6.1
psycopg2==2.8.4
pycairo==1.16.2
pycrypto==2.6.1
pycups==1.9.73
Pygments==2.3.1
PyGObject==3.36.0
PyJWT==1.7.1
pymacaroons==0.13.0
PyNaCl==1.3.0
pyparsing==2.4.6
pyproj==2.5.0
PyQt5==5.14.1
pyRFC3339==1.1
pyrsistent==0.15.5
python-apt==2.0.0+ubuntu0.20.4.1
python-dateutil==2.7.3
python-debian===0.1.36ubuntu1
pytz==2019.3
pyxdg==0.26
PyYAML==5.3.1
reportlab==3.5.34
requests==2.22.0
requests-unixsocket==0.2.0
retrying==1.3.3
scour==0.37
SecretStorage==2.3.1
simplejson==3.16.0
sip==4.19.21
six==1.14.0
soupsieve==1.9.5
speedtest-cli==2.1.2
system-service==0.3
systemd-python==234
traitlets==4.3.3
ubuntu-advantage-tools==20.3
ubuntu-drivers-common==0.0.0
ufw==0.36
unattended-upgrades==0.1
urllib3==1.25.8
usb-creator==0.3.7
virtualenv==20.0.17
wadllib==1.3.3
webencodings==0.5.1
xkit==0.0.0
zipp==1.0.0
zope.interface==4.7.1
  1. What code are you running? Give us a specific commit of a specific repo that we can check out.

Here is the full example, including a scenario that also occurs with a break statement.

example.py

#!/usr/bin/env python3

nums = [1,2,3,4,5,6,7,8]

for num in nums:
    if num % 2 == 0:
        if num % 4 == 0:
            print(num)
        continue
    print(-num)

nums = [1,3,4,5,6,7,8]
for num in nums: # pragma: no branch
    if num % 2 == 0:
        if num % 4 == 0:
            print(num)
        break
    print(-num)
  1. What commands did you run?
coverage run --branch example.py
coverage html

Expected behavior
if statement in the above examples should show as fully covered

Additional context
N/A

Metadata

Metadata

Assignees

No one assigned

    Labels

    duplicateThis issue or pull request already existsnot our bugThe problem was elsewhere

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions