diff --git a/src/_pytest/assertion/rewrite.py b/src/_pytest/assertion/rewrite.py index 4037aa959..a2a4b135c 100644 --- a/src/_pytest/assertion/rewrite.py +++ b/src/_pytest/assertion/rewrite.py @@ -462,7 +462,7 @@ def _format_assertmsg(obj: object) -> str: def _should_repr_global_name(obj: object) -> bool: if callable(obj): - return hasattr(obj, "__pytest_wrapped__") + return hasattr(obj, "_pytestfixturefunction") try: return not hasattr(obj, "__name__") diff --git a/src/_pytest/fixtures.py b/src/_pytest/fixtures.py index 3b31fccf4..71aea5eee 100644 --- a/src/_pytest/fixtures.py +++ b/src/_pytest/fixtures.py @@ -1200,6 +1200,9 @@ class FixtureFunctionDefinition: ): self.name = fixture_function_marker.name or function.__name__ self.__name__ = self.name + # This attribute is only used to check if an arbitrary python object is a fixture. + # Using isinstance on every object in code might execute code that is not intended to be executed. + # Like lazy loaded classes. self._pytestfixturefunction = fixture_function_marker self.__pytest_wrapped__ = _PytestWrapper(function) self.fixture_function_marker = fixture_function_marker diff --git a/testing/test_assertrewrite.py b/testing/test_assertrewrite.py index 8db9dbbe5..b6e4c4112 100644 --- a/testing/test_assertrewrite.py +++ b/testing/test_assertrewrite.py @@ -744,6 +744,23 @@ class TestAssertionRewrite: assert "UnicodeDecodeError" not in msg assert "UnicodeEncodeError" not in msg + def test_assert_fixture(self, pytester: Pytester) -> None: + pytester.makepyfile( + """\ + import pytest + @pytest.fixture + def fixt(): + return 42 + + def test_something(): # missing "fixt" argument + assert fixt == 42 + """ + ) + result = pytester.runpytest() + result.stdout.fnmatch_lines( + ["*assert pytest_fixture() == 42*"] + ) + class TestRewriteOnImport: def test_pycache_is_a_file(self, pytester: Pytester) -> None: