Skip to content

assertion: save/restore hooks on item #6646

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/6646.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Assertion rewriting hooks are (re)stored for the current item, which fixes them being still used after e.g. pytester's :func:`testdir.runpytest <_pytest.pytester.Testdir.runpytest>` etc.
10 changes: 6 additions & 4 deletions src/_pytest/assertion/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from _pytest.assertion import truncate
from _pytest.assertion import util
from _pytest.compat import TYPE_CHECKING
from _pytest.config import hookimpl

if TYPE_CHECKING:
from _pytest.main import Session
Expand Down Expand Up @@ -105,7 +106,8 @@ def pytest_collection(session: "Session") -> None:
assertstate.hook.set_session(session)


def pytest_runtest_setup(item):
@hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_protocol(item):
"""Setup the pytest_assertrepr_compare and pytest_assertion_pass hooks

The newinterpret and rewrite modules will use util._reprcompare if
Expand Down Expand Up @@ -143,6 +145,7 @@ def callbinrepr(op, left, right):
return res
return None

saved_assert_hooks = util._reprcompare, util._assertion_pass
util._reprcompare = callbinrepr

if item.ihook.pytest_assertion_pass.get_hookimpls():
Expand All @@ -154,10 +157,9 @@ def call_assertion_pass_hook(lineno, orig, expl):

util._assertion_pass = call_assertion_pass_hook

yield

def pytest_runtest_teardown(item):
util._reprcompare = None
util._assertion_pass = None
util._reprcompare, util._assertion_pass = saved_assert_hooks


def pytest_sessionfinish(session):
Expand Down
5 changes: 4 additions & 1 deletion src/_pytest/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
from pluggy import PluginManager

import _pytest._code
import _pytest.assertion
import _pytest.deprecated
import _pytest.hookspec # the extension point definitions
from .exceptions import PrintHelp
Expand Down Expand Up @@ -260,6 +259,8 @@ class PytestPluginManager(PluginManager):
"""

def __init__(self):
import _pytest.assertion

super().__init__("pytest")
# The objects are module objects, only used generically.
self._conftest_plugins = set() # type: Set[object]
Expand Down Expand Up @@ -891,6 +892,8 @@ def _consider_importhook(self, args):
ns, unknown_args = self._parser.parse_known_and_unknown_args(args)
mode = getattr(ns, "assertmode", "plain")
if mode == "rewrite":
import _pytest.assertion

try:
hook = _pytest.assertion.install_importhook(self)
except SystemError:
Expand Down
17 changes: 13 additions & 4 deletions testing/test_assertion.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,19 @@ def test_dummy_failure(testdir): # how meta!
result = testdir.runpytest_subprocess()
result.stdout.fnmatch_lines(
[
"E * AssertionError: ([[][]], [[][]], [[]<TestReport *>[]])*",
"E * assert"
" {'failed': 1, 'passed': 0, 'skipped': 0} =="
" {'failed': 0, 'passed': 1, 'skipped': 0}",
"> r.assertoutcome(passed=1)",
"E AssertionError: ([[][]], [[][]], [[]<TestReport *>[]])*",
"E assert {'failed': 1,... 'skipped': 0} == {'failed': 0,... 'skipped': 0}",
"E Omitting 1 identical items, use -vv to show",
"E Differing items:",
"E Use -v to get the full diff",
]
)
# XXX: unstable output.
result.stdout.fnmatch_lines_random(
[
"E {'failed': 1} != {'failed': 0}",
"E {'passed': 0} != {'passed': 1}",
]
)

Expand Down