From 88d1bcc91b2dfbc64623a26ba2ae7bfdc8bd1600 Mon Sep 17 00:00:00 2001 From: Meet Pandya Date: Wed, 19 Aug 2026 18:43:50 +0530 Subject: [PATCH] Improve self fixture lookup error --- changelog/12907.improvement.rst | 1 + src/_pytest/fixtures.py | 5 +++++ testing/python/fixtures.py | 17 +++++++++++++++++ 3 files changed, 23 insertions(+) create mode 100644 changelog/12907.improvement.rst diff --git a/changelog/12907.improvement.rst b/changelog/12907.improvement.rst new file mode 100644 index 00000000000..b53ed1673b3 --- /dev/null +++ b/changelog/12907.improvement.rst @@ -0,0 +1 @@ +The missing fixture error for bare test functions which mistakenly declare a ``self`` parameter now suggests removing it. diff --git a/src/_pytest/fixtures.py b/src/_pytest/fixtures.py index 44c304905e7..d93dde82fbb 100644 --- a/src/_pytest/fixtures.py +++ b/src/_pytest/fixtures.py @@ -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." diff --git a/testing/python/fixtures.py b/testing/python/fixtures.py index f49c41ec9df..0e73662b751 100644 --- a/testing/python/fixtures.py +++ b/testing/python/fixtures.py @@ -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(