Skip to content

Commit ef46a37

Browse files
committed
add test case for asserting missing fixture
1 parent bf41c9f commit ef46a37

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

src/_pytest/assertion/rewrite.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ def _format_assertmsg(obj: object) -> str:
462462

463463
def _should_repr_global_name(obj: object) -> bool:
464464
if callable(obj):
465-
return hasattr(obj, "__pytest_wrapped__")
465+
return hasattr(obj, "_pytestfixturefunction")
466466

467467
try:
468468
return not hasattr(obj, "__name__")

src/_pytest/fixtures.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1200,6 +1200,9 @@ def __init__(
12001200
):
12011201
self.name = fixture_function_marker.name or function.__name__
12021202
self.__name__ = self.name
1203+
# This attribute is only used to check if an arbitrary python object is a fixture.
1204+
# Using isinstance on every object in code might execute code that is not intended to be executed.
1205+
# Like lazy loaded classes.
12031206
self._pytestfixturefunction = fixture_function_marker
12041207
self.__pytest_wrapped__ = _PytestWrapper(function)
12051208
self.fixture_function_marker = fixture_function_marker

testing/test_assertrewrite.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,23 @@ def __repr__(self):
744744
assert "UnicodeDecodeError" not in msg
745745
assert "UnicodeEncodeError" not in msg
746746

747+
def test_assert_fixture(self, pytester: Pytester) -> None:
748+
pytester.makepyfile(
749+
"""\
750+
import pytest
751+
@pytest.fixture
752+
def fixt():
753+
return 42
754+
755+
def test_something(): # missing "fixt" argument
756+
assert fixt == 42
757+
"""
758+
)
759+
result = pytester.runpytest()
760+
result.stdout.fnmatch_lines(
761+
["*assert pytest_fixture(<function fixt at *>) == 42*"]
762+
)
763+
747764

748765
class TestRewriteOnImport:
749766
def test_pycache_is_a_file(self, pytester: Pytester) -> None:

0 commit comments

Comments
 (0)