Skip to content

Commit 1cbafeb

Browse files
author
Victor Maryama
committed
Little refactoring and added tests for case when explanation should not be generated.
1 parent ed8aed6 commit 1cbafeb

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

src/_pytest/assertion/rewrite.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,7 @@ def _call_assertion_pass(lineno, orig, expl):
543543
def _check_if_assertionpass_impl():
544544
"""Checks if any plugins implement the pytest_assertion_pass hook
545545
in order not to generate explanation unecessarily (might be expensive)"""
546-
return True if util._assertion_pass is not None else False
546+
return True if util._assertion_pass else False
547547

548548

549549
unary_map = {ast.Not: "not %s", ast.Invert: "~%s", ast.USub: "-%s", ast.UAdd: "+%s"}
@@ -883,7 +883,6 @@ def visit_Assert(self, assert_):
883883
[],
884884
)
885885
main_test = ast.If(negation, [raise_], [hook_impl_test])
886-
# main_test = ast.If(negation, [raise_], [hook_call_pass])
887886

888887
self.statements.extend(self.expl_stmts)
889888
self.statements.append(main_test)

testing/test_assertrewrite.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1474,3 +1474,28 @@ def test_simple():
14741474
result.stdout.fnmatch_lines(
14751475
"*Assertion Passed: a + b == c + d (1 + 2) == (3 + 0) at line 7*"
14761476
)
1477+
1478+
def test_hook_not_called_without_hookimpl(self, testdir, monkeypatch):
1479+
"""Assertion pass should not be called (and hence formatting should
1480+
not occur) if there is no hook declared for pytest_assertion_pass"""
1481+
1482+
def raise_on_assertionpass(*_, **__):
1483+
raise Exception("Assertion passed called when it shouldn't!")
1484+
1485+
monkeypatch.setattr(_pytest.assertion.rewrite,
1486+
"_call_assertion_pass",
1487+
raise_on_assertionpass)
1488+
1489+
testdir.makepyfile(
1490+
"""
1491+
def test_simple():
1492+
a=1
1493+
b=2
1494+
c=3
1495+
d=0
1496+
1497+
assert a+b == c+d
1498+
"""
1499+
)
1500+
result = testdir.runpytest()
1501+
result.assert_outcomes(passed=1)

0 commit comments

Comments
 (0)