Skip to content
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
11 changes: 6 additions & 5 deletions src/_pytest/deprecated.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,13 @@
"Use @pytest.fixture instead; they are the same."
)

CLASS_FIXTURE_INSTANCE_METHOD = PytestRemovedIn10Warning(
"Class-scoped fixture defined as instance method is deprecated.\n"
"Instance attributes set in this fixture will NOT be visible to test methods,\n"
CLASS_FIXTURE_INSTANCE_METHOD = UnformattedWarning(
PytestRemovedIn10Warning,
"Class-scoped fixtures defined as instance methods are deprecated.\n"
"Instance attributes set in the {fixturename!r} fixture will NOT be visible to test methods,\n"
"as each test gets a new instance while the fixture runs only once per class.\n"
"Use @classmethod decorator and set attributes on cls instead.\n"
"See https://docs.pytest.org/en/stable/deprecations.html#class-scoped-fixture-as-instance-method"
"Use a @classmethod decorator below @pytest.fixture and set attributes on cls instead.\n"
"See https://docs.pytest.org/en/stable/deprecations.html#class-scoped-fixture-as-instance-method",
)

# This deprecation is never really meant to be removed.
Expand Down
11 changes: 10 additions & 1 deletion src/_pytest/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -1274,6 +1274,10 @@ def resolve_fixture_function(
) -> _FixtureFunc[FixtureValue]:
"""Get the actual callable that can be called to obtain the fixture
value."""

def __tracebackhide__(e):
return issubclass(e.type, CLASS_FIXTURE_INSTANCE_METHOD.category)

fixturefunc = fixturedef.func
# The fixture function needs to be bound to the actual
# request.instance so that code working with "fixturedef" behaves
Expand All @@ -1287,7 +1291,12 @@ def resolve_fixture_function(
# classmethod: bound_to is the class itself (a type)
# instance method: bound_to is an instance (not a type)
if not isinstance(bound_to, type):
warnings.warn(CLASS_FIXTURE_INSTANCE_METHOD, stacklevel=2)
warnings.warn(
CLASS_FIXTURE_INSTANCE_METHOD.format(
fixturename=request.fixturename
),
stacklevel=2,
)

if instance is not None:
# Handle the case where fixture is defined not in a test class, but some other class
Expand Down
4 changes: 3 additions & 1 deletion testing/deprecated_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,9 @@ def test_foo(self, fix):
result = pytester.runpytest("-Werror::pytest.PytestRemovedIn10Warning")
result.assert_outcomes(errors=1)
result.stdout.fnmatch_lines(
["*PytestRemovedIn10Warning: Class-scoped fixture defined as instance method*"]
[
"*PytestRemovedIn10Warning: Class-scoped fixtures defined as instance methods*"
]
)


Expand Down