Closed
Description
I've been testing with Python 3.10a4 and noticed a case where code coverage differs in 3.10. The branch from line 5->9 is not marked as covered when fetch_result
is False.
def f(cursor, fetch_result=True):
result = Result()
try:
cursor.execute('...')
if fetch_result: # branch from here to return is not recorded
result.fetch_from(cursor)
except Exception:
raise BadThingHappened()
return result
The coverage miss seems to be related to the branch coming out of the try
-except
block.
To Reproduce
The minimal test case that I have come up with is:
import time
class Cursor:
def execute(self, query):
print('querying')
time.sleep(0.1)
def fetch(self):
print('fetching result')
time.sleep(0.1)
class Result:
def fetch_from(self, cursor):
cursor.fetch()
class BadThingHappened(Exception):
pass
def f(cursor, fetch_result=True):
result = Result()
try:
cursor.execute('...')
if fetch_result:
result.fetch_from(cursor)
except Exception:
raise BadThingHappened()
return result
import unittest.mock
import simple
class TestTheFunction(unittest.TestCase):
def test_with_fetch_result(self):
simple.f(simple.Cursor())
def test_without_fetch_result(self):
simple.f(simple.Cursor(), False)
def test_failure_cases(self):
cursor = unittest.mock.Mock(spec=simple.Cursor)
cursor.execute.side_effect = RuntimeError
with self.assertRaises(simple.BadThingHappened):
simple.f(cursor)
with self.assertRaises(simple.BadThingHappened):
simple.f(cursor, False)
Output from Python 3.9
$ .tox/py39/bin/python --version
Python 3.9.1
$ .tox/py39/bin/coverage --version
Coverage.py, version 5.3.1 with C extension
Full documentation is at https://coverage.readthedocs.io
$ .tox/py39/bin/coverage run --source simple --branch -m unittest
...
----------------------------------------------------------------------
Ran 3 tests in 0.322s
OK
$ .tox/py39/bin/coverage report --show-missing
Name Stmts Miss Branch BrPart Cover Missing
-------------------------------------------------------
simple.py 20 0 2 0 100%
$
Output from Python 3.10
$ .tox/py310/bin/python --version
Python 3.10.0a4
$ .tox/py310/bin/coverage --version
Coverage.py, version 5.3.1 with C extension
Full documentation is at https://coverage.readthedocs.io
$ .tox/py310/bin/coverage run --source simple --branch -m unittest
...
----------------------------------------------------------------------
Ran 3 tests in 0.323s
OK
$ .tox/py310/bin/coverage report --show-missing
Name Stmts Miss Branch BrPart Cover Missing
-------------------------------------------------------
simple.py 20 0 2 1 95% 25->29
$