33
44from _pytest import deprecated
55from _pytest .pytester import Pytester
6+ from _pytest .scope import Scope
67import pytest
78from pytest import PytestDeprecationWarning
89
@@ -87,13 +88,18 @@ def __init__(self, foo: int, *, _ispytest: bool = False) -> None:
8788 PrivateInit (10 , _ispytest = True )
8889
8990
90- def test_class_scope_instance_method_is_deprecated (pytester : Pytester ) -> None :
91+ @pytest .mark .parametrize (
92+ "scope" , [Scope .Class , Scope .Module , Scope .Package , Scope .Session ]
93+ )
94+ def test_higher_scope_instance_method_is_deprecated (
95+ pytester : Pytester , scope : Scope
96+ ) -> None :
9197 pytester .makepyfile (
92- """
98+ f """
9399 import pytest
94100
95101 class TestClass:
96- @pytest.fixture(scope="class ")
102+ @pytest.fixture(scope="{ scope . value } ")
97103 def fix(self):
98104 self.attr = True
99105
@@ -104,20 +110,23 @@ def test_foo(self, fix):
104110 result = pytester .runpytest ("-Werror::pytest.PytestRemovedIn10Warning" )
105111 result .assert_outcomes (errors = 1 )
106112 result .stdout .fnmatch_lines (
107- [
108- "*PytestRemovedIn10Warning: Class-scoped fixtures defined as instance methods*"
109- ]
113+ ["*PytestRemovedIn10Warning: *-scoped fixtures defined as instance methods*" ]
110114 )
111115
112116
113- def test_class_scope_classmethod_fixture_not_deprecated (pytester : Pytester ) -> None :
114- """A class-scoped fixture defined as @classmethod does NOT warn."""
117+ @pytest .mark .parametrize (
118+ "scope" , [Scope .Class , Scope .Module , Scope .Package , Scope .Session ]
119+ )
120+ def test_higher_scope_classmethod_fixture_not_deprecated (
121+ pytester : Pytester , scope : Scope
122+ ) -> None :
123+ """A higher-scoped fixture defined as @classmethod does NOT warn."""
115124 pytester .makepyfile (
116- """
125+ f """
117126 import pytest
118127
119128 class TestClass:
120- @pytest.fixture(scope="class ")
129+ @pytest.fixture(scope="{ scope . value } ")
121130 @classmethod
122131 def fix(cls):
123132 cls.attr = True
@@ -130,6 +139,27 @@ def test_foo(self, fix):
130139 result .assert_outcomes (passed = 1 )
131140
132141
142+ @pytest .mark .parametrize ("scope" , list (Scope ))
143+ def test_staticmethod_fixture_not_deprecated (pytester : Pytester , scope : Scope ) -> None :
144+ """A fixture at any scope defined as @staticmethod does NOT warn."""
145+ pytester .makepyfile (
146+ f"""
147+ import pytest
148+
149+ class TestClass:
150+ @pytest.fixture(scope="{ scope .value } ")
151+ @staticmethod
152+ def fix():
153+ pass
154+
155+ def test_foo(self, fix):
156+ pass
157+ """
158+ )
159+ result = pytester .runpytest ("-Werror::pytest.PytestRemovedIn10Warning" )
160+ result .assert_outcomes (passed = 1 )
161+
162+
133163class TestFixtureNodeidDeprecations :
134164 """Tests for deprecated baseid/nodeid string APIs in fixture registration.
135165
0 commit comments