Skip to content

Commit 090f7ff

Browse files
committed
Remove request.cached_setup
Fix #4489
1 parent 06dc6e3 commit 090f7ff

File tree

6 files changed

+38
-284
lines changed

6 files changed

+38
-284
lines changed

changelog/4489.removal.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Removed ``request.cached_setup``. This was the predecessor mechanism to modern fixtures.
2+
3+
See our `docs <https://docs.pytest.org/en/latest/deprecations.html#cached-setup>`__ on information on how to update your code.

doc/en/deprecations.rst

Lines changed: 33 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -49,41 +49,6 @@ Becomes:
4949
exec("assert(1, 2)") # exec is used to avoid a top-level warning
5050
5151
52-
53-
``cached_setup``
54-
~~~~~~~~~~~~~~~~
55-
56-
.. deprecated:: 3.9
57-
58-
``request.cached_setup`` was the precursor of the setup/teardown mechanism available to fixtures.
59-
60-
Example:
61-
62-
.. code-block:: python
63-
64-
@pytest.fixture
65-
def db_session():
66-
return request.cached_setup(
67-
setup=Session.create, teardown=lambda session: session.close(), scope="module"
68-
)
69-
70-
This should be updated to make use of standard fixture mechanisms:
71-
72-
.. code-block:: python
73-
74-
@pytest.fixture(scope="module")
75-
def db_session():
76-
session = Session.create()
77-
yield session
78-
session.close()
79-
80-
81-
You can consult `funcarg comparison section in the docs <https://docs.pytest.org/en/latest/funcarg_compare.html>`_ for
82-
more information.
83-
84-
This has been documented as deprecated for years, but only now we are actually emitting deprecation warnings.
85-
86-
8752
Using ``Class`` in custom Collectors
8853
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8954

@@ -309,6 +274,39 @@ Removed Features
309274
As stated in our :ref:`backwards-compatibility` policy, deprecated features are removed only in major releases after
310275
an appropriate period of deprecation has passed.
311276

277+
``cached_setup``
278+
~~~~~~~~~~~~~~~~
279+
280+
*Removed in version 4.0.*
281+
282+
``request.cached_setup`` was the precursor of the setup/teardown mechanism available to fixtures.
283+
284+
Example:
285+
286+
.. code-block:: python
287+
288+
@pytest.fixture
289+
def db_session():
290+
return request.cached_setup(
291+
setup=Session.create, teardown=lambda session: session.close(), scope="module"
292+
)
293+
294+
This should be updated to make use of standard fixture mechanisms:
295+
296+
.. code-block:: python
297+
298+
@pytest.fixture(scope="module")
299+
def db_session():
300+
session = Session.create()
301+
yield session
302+
session.close()
303+
304+
305+
You can consult `funcarg comparison section in the docs <https://docs.pytest.org/en/latest/funcarg_compare.html>`_ for
306+
more information.
307+
308+
This has been documented as deprecated for years, but only now we are actually emitting deprecation warnings.
309+
312310
``yield`` tests
313311
~~~~~~~~~~~~~~~
314312

src/_pytest/deprecated.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,6 @@
2424

2525
YIELD_TESTS = "yield tests were removed in pytest 4.0 - {name} will be ignored"
2626

27-
CACHED_SETUP = RemovedInPytest4Warning(
28-
"cached_setup is deprecated and will be removed in a future release. "
29-
"Use standard fixture functions instead."
30-
)
31-
3227
FUNCARG_PREFIX = UnformattedWarning(
3328
RemovedInPytest4Warning,
3429
'{name}: declaring fixtures using "pytest_funcarg__" prefix is deprecated '

src/_pytest/fixtures.py

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -469,43 +469,6 @@ def _fillfixtures(self):
469469
if argname not in item.funcargs:
470470
item.funcargs[argname] = self.getfixturevalue(argname)
471471

472-
def cached_setup(self, setup, teardown=None, scope="module", extrakey=None):
473-
""" (deprecated) Return a testing resource managed by ``setup`` &
474-
``teardown`` calls. ``scope`` and ``extrakey`` determine when the
475-
``teardown`` function will be called so that subsequent calls to
476-
``setup`` would recreate the resource. With pytest-2.3 you often
477-
do not need ``cached_setup()`` as you can directly declare a scope
478-
on a fixture function and register a finalizer through
479-
``request.addfinalizer()``.
480-
481-
:arg teardown: function receiving a previously setup resource.
482-
:arg setup: a no-argument function creating a resource.
483-
:arg scope: a string value out of ``function``, ``class``, ``module``
484-
or ``session`` indicating the caching lifecycle of the resource.
485-
:arg extrakey: added to internal caching key of (funcargname, scope).
486-
"""
487-
from _pytest.deprecated import CACHED_SETUP
488-
489-
warnings.warn(CACHED_SETUP, stacklevel=2)
490-
if not hasattr(self.config, "_setupcache"):
491-
self.config._setupcache = {} # XXX weakref?
492-
cachekey = (self.fixturename, self._getscopeitem(scope), extrakey)
493-
cache = self.config._setupcache
494-
try:
495-
val = cache[cachekey]
496-
except KeyError:
497-
self._check_scope(self.fixturename, self.scope, scope)
498-
val = setup()
499-
cache[cachekey] = val
500-
if teardown is not None:
501-
502-
def finalizer():
503-
del cache[cachekey]
504-
teardown(val)
505-
506-
self._addfinalizer(finalizer, scope=scope)
507-
return val
508-
509472
def getfixturevalue(self, argname):
510473
""" Dynamically run a named fixture function.
511474

testing/deprecated_test.py

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,6 @@
1010
pytestmark = pytest.mark.pytester_example_path("deprecated")
1111

1212

13-
def test_cached_setup_deprecation(testdir):
14-
testdir.makepyfile(
15-
"""
16-
import pytest
17-
@pytest.fixture
18-
def fix(request):
19-
return request.cached_setup(lambda: 1)
20-
21-
def test_foo(fix):
22-
assert fix == 1
23-
"""
24-
)
25-
result = testdir.runpytest(SHOW_PYTEST_WARNINGS_ARG)
26-
result.stdout.fnmatch_lines(
27-
[
28-
"*test_cached_setup_deprecation.py:4:*cached_setup is deprecated*",
29-
"*1 passed, 1 warnings in*",
30-
]
31-
)
32-
33-
3413
def test_funcarg_prefix_deprecation(testdir):
3514
testdir.makepyfile(
3615
"""

testing/python/fixture.py

Lines changed: 2 additions & 186 deletions
Original file line numberDiff line numberDiff line change
@@ -953,181 +953,6 @@ def test_fun2(keywords):
953953
reprec.assertoutcome(passed=2)
954954

955955

956-
class TestRequestCachedSetup(object):
957-
def test_request_cachedsetup_defaultmodule(self, testdir):
958-
reprec = testdir.inline_runsource(
959-
"""
960-
mysetup = ["hello",].pop
961-
962-
import pytest
963-
964-
@pytest.fixture
965-
def something(request):
966-
return request.cached_setup(mysetup, scope="module")
967-
968-
def test_func1(something):
969-
assert something == "hello"
970-
class TestClass(object):
971-
def test_func1a(self, something):
972-
assert something == "hello"
973-
""",
974-
SHOW_PYTEST_WARNINGS_ARG,
975-
)
976-
reprec.assertoutcome(passed=2)
977-
978-
def test_request_cachedsetup_class(self, testdir):
979-
reprec = testdir.inline_runsource(
980-
"""
981-
mysetup = ["hello", "hello2", "hello3"].pop
982-
983-
import pytest
984-
@pytest.fixture
985-
def something(request):
986-
return request.cached_setup(mysetup, scope="class")
987-
def test_func1(something):
988-
assert something == "hello3"
989-
def test_func2(something):
990-
assert something == "hello2"
991-
class TestClass(object):
992-
def test_func1a(self, something):
993-
assert something == "hello"
994-
def test_func2b(self, something):
995-
assert something == "hello"
996-
""",
997-
SHOW_PYTEST_WARNINGS_ARG,
998-
)
999-
reprec.assertoutcome(passed=4)
1000-
1001-
@pytest.mark.filterwarnings("ignore:cached_setup is deprecated")
1002-
def test_request_cachedsetup_extrakey(self, testdir):
1003-
item1 = testdir.getitem("def test_func(): pass")
1004-
req1 = fixtures.FixtureRequest(item1)
1005-
values = ["hello", "world"]
1006-
1007-
def setup():
1008-
return values.pop()
1009-
1010-
ret1 = req1.cached_setup(setup, extrakey=1)
1011-
ret2 = req1.cached_setup(setup, extrakey=2)
1012-
assert ret2 == "hello"
1013-
assert ret1 == "world"
1014-
ret1b = req1.cached_setup(setup, extrakey=1)
1015-
ret2b = req1.cached_setup(setup, extrakey=2)
1016-
assert ret1 == ret1b
1017-
assert ret2 == ret2b
1018-
1019-
@pytest.mark.filterwarnings("ignore:cached_setup is deprecated")
1020-
def test_request_cachedsetup_cache_deletion(self, testdir):
1021-
item1 = testdir.getitem("def test_func(): pass")
1022-
req1 = fixtures.FixtureRequest(item1)
1023-
values = []
1024-
1025-
def setup():
1026-
values.append("setup")
1027-
1028-
def teardown(val):
1029-
values.append("teardown")
1030-
1031-
req1.cached_setup(setup, teardown, scope="function")
1032-
assert values == ["setup"]
1033-
# artificial call of finalizer
1034-
setupstate = req1._pyfuncitem.session._setupstate
1035-
setupstate._callfinalizers(item1)
1036-
assert values == ["setup", "teardown"]
1037-
req1.cached_setup(setup, teardown, scope="function")
1038-
assert values == ["setup", "teardown", "setup"]
1039-
setupstate._callfinalizers(item1)
1040-
assert values == ["setup", "teardown", "setup", "teardown"]
1041-
1042-
def test_request_cached_setup_two_args(self, testdir):
1043-
testdir.makepyfile(
1044-
"""
1045-
import pytest
1046-
1047-
@pytest.fixture
1048-
def arg1(request):
1049-
return request.cached_setup(lambda: 42)
1050-
@pytest.fixture
1051-
def arg2(request):
1052-
return request.cached_setup(lambda: 17)
1053-
def test_two_different_setups(arg1, arg2):
1054-
assert arg1 != arg2
1055-
"""
1056-
)
1057-
result = testdir.runpytest("-v", SHOW_PYTEST_WARNINGS_ARG)
1058-
result.stdout.fnmatch_lines(["*1 passed*"])
1059-
1060-
def test_request_cached_setup_getfixturevalue(self, testdir):
1061-
testdir.makepyfile(
1062-
"""
1063-
import pytest
1064-
1065-
@pytest.fixture
1066-
def arg1(request):
1067-
arg1 = request.getfixturevalue("arg2")
1068-
return request.cached_setup(lambda: arg1 + 1)
1069-
@pytest.fixture
1070-
def arg2(request):
1071-
return request.cached_setup(lambda: 10)
1072-
def test_two_funcarg(arg1):
1073-
assert arg1 == 11
1074-
"""
1075-
)
1076-
result = testdir.runpytest("-v", SHOW_PYTEST_WARNINGS_ARG)
1077-
result.stdout.fnmatch_lines(["*1 passed*"])
1078-
1079-
def test_request_cached_setup_functional(self, testdir):
1080-
testdir.makepyfile(
1081-
test_0="""
1082-
import pytest
1083-
values = []
1084-
@pytest.fixture
1085-
def something(request):
1086-
val = request.cached_setup(fsetup, fteardown)
1087-
return val
1088-
def fsetup(mycache=[1]):
1089-
values.append(mycache.pop())
1090-
return values
1091-
def fteardown(something):
1092-
values.remove(something[0])
1093-
values.append(2)
1094-
def test_list_once(something):
1095-
assert something == [1]
1096-
def test_list_twice(something):
1097-
assert something == [1]
1098-
"""
1099-
)
1100-
testdir.makepyfile(
1101-
test_1="""
1102-
import test_0 # should have run already
1103-
def test_check_test0_has_teardown_correct():
1104-
assert test_0.values == [2]
1105-
"""
1106-
)
1107-
result = testdir.runpytest("-v", SHOW_PYTEST_WARNINGS_ARG)
1108-
result.stdout.fnmatch_lines(["*3 passed*"])
1109-
1110-
def test_issue117_sessionscopeteardown(self, testdir):
1111-
testdir.makepyfile(
1112-
"""
1113-
import pytest
1114-
1115-
@pytest.fixture
1116-
def app(request):
1117-
app = request.cached_setup(
1118-
scope='session',
1119-
setup=lambda: 0,
1120-
teardown=lambda x: 3/x)
1121-
return app
1122-
def test_func(app):
1123-
pass
1124-
"""
1125-
)
1126-
result = testdir.runpytest(SHOW_PYTEST_WARNINGS_ARG)
1127-
assert result.ret != 0
1128-
result.stdout.fnmatch_lines(["*3/x*", "*ZeroDivisionError*"])
1129-
1130-
1131956
class TestFixtureUsages(object):
1132957
def test_noargfixturedec(self, testdir):
1133958
testdir.makepyfile(
@@ -2297,15 +2122,7 @@ def test_4(arg, created, finalized):
22972122
reprec = testdir.inline_run()
22982123
reprec.assertoutcome(passed=4)
22992124

2300-
@pytest.mark.parametrize(
2301-
"method",
2302-
[
2303-
'request.getfixturevalue("arg")',
2304-
'request.cached_setup(lambda: None, scope="function")',
2305-
],
2306-
ids=["getfixturevalue", "cached_setup"],
2307-
)
2308-
def test_scope_mismatch_various(self, testdir, method):
2125+
def test_scope_mismatch_various(self, testdir):
23092126
testdir.makeconftest(
23102127
"""
23112128
import pytest
@@ -2321,11 +2138,10 @@ def arg(request):
23212138
import pytest
23222139
@pytest.fixture(scope="session")
23232140
def arg(request):
2324-
%s
2141+
request.getfixturevalue("arg")
23252142
def test_1(arg):
23262143
pass
23272144
"""
2328-
% method
23292145
)
23302146
result = testdir.runpytest(SHOW_PYTEST_WARNINGS_ARG)
23312147
assert result.ret != 0

0 commit comments

Comments
 (0)