Skip to content
Open
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/12907.improvement.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The missing fixture error for bare test functions which mistakenly declare a ``self`` parameter now suggests removing it.
5 changes: 5 additions & 0 deletions src/_pytest/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -1009,6 +1009,11 @@ def formatrepr(self) -> FixtureLookupErrorRepr:
)
else:
msg = f"fixture '{self.argname}' not found"
if self.argname == "self":
msg += (
"\n hint: remove 'self' from the function definition if this "
"is not a method"
)
msg += "\n available fixtures: {}".format(", ".join(sorted(available)))
msg += "\n use 'pytest --fixtures [testpath]' for help on them."

Expand Down
17 changes: 17 additions & 0 deletions testing/python/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,23 @@ def test_lookup_error(unknown):
)
result.stdout.no_fnmatch_line("*INTERNAL*")

def test_funcarg_lookup_error_self_hint(self, pytester: Pytester) -> None:
pytester.makepyfile(
"""
def test_something(self):
pass
"""
)
result = pytester.runpytest()
result.stdout.fnmatch_lines(
[
"*ERROR at setup of test_something*",
"E fixture 'self' not found",
"> hint: remove 'self' from the function definition if this is not a method",
]
)
result.assert_outcomes(errors=1)

def test_fixture_excinfo_leak(self, pytester: Pytester) -> None:
# on python2 sys.excinfo would leak into fixture executions
pytester.makepyfile(
Expand Down
Loading