diff --git a/changelog/3616.deprecation.rst b/changelog/3616.deprecation.rst index e02e83c20..e91d1ad07 100644 --- a/changelog/3616.deprecation.rst +++ b/changelog/3616.deprecation.rst @@ -6,3 +6,6 @@ The following accesses have been documented as deprecated for years, but are now usage of Function.Module is deprecated, please use pytest.Module instead Users should just ``import pytest`` and access those objects using the ``pytest`` module. + +* ``request.cached_setup``, this was the precursor of the setup/teardown mechanism available to fixtures. You can + consult `funcarg comparision section in the docs `_. diff --git a/src/_pytest/fixtures.py b/src/_pytest/fixtures.py index 068e6814c..f2c8085ed 100644 --- a/src/_pytest/fixtures.py +++ b/src/_pytest/fixtures.py @@ -479,6 +479,11 @@ class FixtureRequest(FuncargnamesCompatAttr): or ``session`` indicating the caching lifecycle of the resource. :arg extrakey: added to internal caching key of (funcargname, scope). """ + msg = ( + "cached_setup is deprecated and will be removed in a future release. " + "Use standard fixture functions instead." + ) + warnings.warn(RemovedInPytest4Warning(msg), stacklevel=2) if not hasattr(self.config, "_setupcache"): self.config._setupcache = {} # XXX weakref? cachekey = (self.fixturename, self._getscopeitem(scope), extrakey) diff --git a/testing/deprecated_test.py b/testing/deprecated_test.py index d53f86e15..7ca8e6bae 100644 --- a/testing/deprecated_test.py +++ b/testing/deprecated_test.py @@ -47,6 +47,27 @@ def test_compat_properties_deprecation(testdir): ) +def test_cached_setup_deprecation(testdir): + testdir.makepyfile( + """ + import pytest + @pytest.fixture + def fix(request): + return request.cached_setup(lambda: 1) + + def test_foo(fix): + assert fix == 1 + """ + ) + result = testdir.runpytest() + result.stdout.fnmatch_lines( + [ + "*test_cached_setup_deprecation.py:4:*cached_setup is deprecated*", + "*1 passed, 1 warnings in*", + ] + ) + + @pytest.mark.filterwarnings("default") def test_funcarg_prefix_deprecation(testdir): testdir.makepyfile( diff --git a/testing/python/fixture.py b/testing/python/fixture.py index fc3eee42b..4e44bf961 100644 --- a/testing/python/fixture.py +++ b/testing/python/fixture.py @@ -977,6 +977,7 @@ class TestRequestCachedSetup(object): ) reprec.assertoutcome(passed=4) + @pytest.mark.filterwarnings("ignore:cached_setup is deprecated") def test_request_cachedsetup_extrakey(self, testdir): item1 = testdir.getitem("def test_func(): pass") req1 = fixtures.FixtureRequest(item1) @@ -994,6 +995,7 @@ class TestRequestCachedSetup(object): assert ret1 == ret1b assert ret2 == ret2b + @pytest.mark.filterwarnings("ignore:cached_setup is deprecated") def test_request_cachedsetup_cache_deletion(self, testdir): item1 = testdir.getitem("def test_func(): pass") req1 = fixtures.FixtureRequest(item1)