Skip to content

Commit 32dac18

Browse files
authored
Merge pull request #5755 from blueyed/fix-assert-location-with-coverage
Fix wrong location of assertion error with Coverage.py .  Reverts using-constant part from 39ba996. Fixes #5754.
2 parents c3a8e60 + e5f4c47 commit 32dac18

File tree

2 files changed

+28
-9
lines changed

2 files changed

+28
-9
lines changed

src/_pytest/assertion/rewrite.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,6 @@
3333
PYC_EXT = ".py" + (__debug__ and "c" or "o")
3434
PYC_TAIL = "." + PYTEST_TAG + PYC_EXT
3535

36-
AST_IS = ast.Is()
37-
AST_NONE = ast.NameConstant(None)
38-
3936

4037
class AssertionRewritingHook:
4138
"""PEP302/PEP451 import hook which rewrites asserts."""
@@ -857,7 +854,7 @@ def warn_about_none_ast(self, node, module_path, lineno):
857854
internally already.
858855
See issue #3191 for more details.
859856
"""
860-
val_is_none = ast.Compare(node, [AST_IS], [AST_NONE])
857+
val_is_none = ast.Compare(node, [ast.Is()], [ast.NameConstant(None)])
861858
send_warning = ast.parse(
862859
"""\
863860
from _pytest.warning_types import PytestAssertRewriteWarning

testing/test_assertion.py

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,6 @@ def test_frozenzet(self):
490490
assert len(expl) > 1
491491

492492
def test_Sequence(self):
493-
494493
if not hasattr(collections_abc, "MutableSequence"):
495494
pytest.skip("cannot import MutableSequence")
496495
MutableSequence = collections_abc.MutableSequence
@@ -806,9 +805,6 @@ def test_fmt_multi_newline_before_where(self):
806805

807806

808807
class TestTruncateExplanation:
809-
810-
""" Confirm assertion output is truncated as expected """
811-
812808
# The number of lines in the truncation explanation message. Used
813809
# to calculate that results have the expected length.
814810
LINES_IN_TRUNCATION_MSG = 2
@@ -969,7 +965,13 @@ def test_hello():
969965
)
970966
result = testdir.runpytest()
971967
result.stdout.fnmatch_lines(
972-
["*def test_hello():*", "*assert x == y*", "*E*Extra items*left*", "*E*50*"]
968+
[
969+
"*def test_hello():*",
970+
"*assert x == y*",
971+
"*E*Extra items*left*",
972+
"*E*50*",
973+
"*= 1 failed in*",
974+
]
973975
)
974976

975977

@@ -1302,3 +1304,23 @@ def raise_exit(obj):
13021304

13031305
with pytest.raises(outcomes.Exit, match="Quitting debugger"):
13041306
callequal(1, 1)
1307+
1308+
1309+
def test_assertion_location_with_coverage(testdir):
1310+
"""This used to report the wrong location when run with coverage (#5754)."""
1311+
p = testdir.makepyfile(
1312+
"""
1313+
def test():
1314+
assert False, 1
1315+
assert False, 2
1316+
"""
1317+
)
1318+
result = testdir.runpytest(str(p))
1319+
result.stdout.fnmatch_lines(
1320+
[
1321+
"> assert False, 1",
1322+
"E AssertionError: 1",
1323+
"E assert False",
1324+
"*= 1 failed in*",
1325+
]
1326+
)

0 commit comments

Comments
 (0)