Skip to content

Commit 4c0513b

Browse files
committed
fixtures: deprecate pytest.yield_fixture()
1 parent 3bcd316 commit 4c0513b

File tree

7 files changed

+51
-42
lines changed

7 files changed

+51
-42
lines changed

changelog/7988.deprecation.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
The ``@pytest.yield_fixture`` decorator/function is now deprecated. Use :func:`pytest.fixture` instead.
2+
3+
``yield_fixture`` has been an alias for ``fixture`` for a very long time, so can be search/replaced safely.

doc/en/deprecations.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,15 @@ flag for all strictness related options (``--strict-markers`` and ``--strict-con
3131
at the moment, more might be introduced in the future).
3232

3333

34+
The ``yield_fixture`` function/decorator
35+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
36+
37+
.. deprecated:: 6.2
38+
39+
``pytest.yield_fixture`` is a deprecated alias for :func:`pytest.fixture`.
40+
41+
It has been so for a very long time, so can be search/replaced safely.
42+
3443

3544
The ``pytest_warning_captured`` hook
3645
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

src/_pytest/deprecated.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@
3232
"Please update to the new name.",
3333
)
3434

35+
YIELD_FIXTURE = PytestDeprecationWarning(
36+
"@pytest.yield_fixture is deprecated.\n"
37+
"Use @pytest.fixture instead; they are the same."
38+
)
3539

3640
MINUS_K_DASH = PytestDeprecationWarning(
3741
"The `-k '-expr'` syntax to -k is deprecated.\nUse `-k 'not expr'` instead."

src/_pytest/fixtures.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
from _pytest.config import Config
5151
from _pytest.config.argparsing import Parser
5252
from _pytest.deprecated import FILLFUNCARGS
53+
from _pytest.deprecated import YIELD_FIXTURE
5354
from _pytest.mark import Mark
5455
from _pytest.mark import ParameterSet
5556
from _pytest.mark.structures import MarkDecorator
@@ -1339,6 +1340,7 @@ def yield_fixture(
13391340
.. deprecated:: 3.0
13401341
Use :py:func:`pytest.fixture` directly instead.
13411342
"""
1343+
warnings.warn(YIELD_FIXTURE, stacklevel=2)
13421344
return fixture(
13431345
fixture_function,
13441346
*args,

testing/deprecated_test.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,11 @@ def test_foo(): pass
115115
"*PytestDeprecationWarning: The --strict option is deprecated, use --strict-markers instead.",
116116
]
117117
)
118+
119+
120+
def test_yield_fixture_is_deprecated() -> None:
121+
with pytest.warns(DeprecationWarning, match=r"yield_fixture is deprecated"):
122+
123+
@pytest.yield_fixture
124+
def fix():
125+
assert False

testing/python/fixtures.py

Lines changed: 19 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from _pytest.config import ExitCode
99
from _pytest.fixtures import FixtureRequest
1010
from _pytest.pytester import get_public_names
11+
from _pytest.pytester import Testdir
1112

1213

1314
def test_getfuncargnames_functions():
@@ -3526,28 +3527,11 @@ def foo():
35263527

35273528

35283529
class TestContextManagerFixtureFuncs:
3529-
@pytest.fixture(params=["fixture", "yield_fixture"])
3530-
def flavor(self, request, testdir, monkeypatch):
3531-
monkeypatch.setenv("PYTEST_FIXTURE_FLAVOR", request.param)
3532-
testdir.makepyfile(
3533-
test_context="""
3534-
import os
3535-
import pytest
3536-
import warnings
3537-
VAR = "PYTEST_FIXTURE_FLAVOR"
3538-
if VAR not in os.environ:
3539-
warnings.warn("PYTEST_FIXTURE_FLAVOR was not set, assuming fixture")
3540-
fixture = pytest.fixture
3541-
else:
3542-
fixture = getattr(pytest, os.environ[VAR])
3543-
"""
3544-
)
3545-
3546-
def test_simple(self, testdir, flavor):
3530+
def test_simple(self, testdir: Testdir) -> None:
35473531
testdir.makepyfile(
35483532
"""
3549-
from test_context import fixture
3550-
@fixture
3533+
import pytest
3534+
@pytest.fixture
35513535
def arg1():
35523536
print("setup")
35533537
yield 1
@@ -3571,11 +3555,11 @@ def test_2(arg1):
35713555
"""
35723556
)
35733557

3574-
def test_scoped(self, testdir, flavor):
3558+
def test_scoped(self, testdir: Testdir) -> None:
35753559
testdir.makepyfile(
35763560
"""
3577-
from test_context import fixture
3578-
@fixture(scope="module")
3561+
import pytest
3562+
@pytest.fixture(scope="module")
35793563
def arg1():
35803564
print("setup")
35813565
yield 1
@@ -3596,11 +3580,11 @@ def test_2(arg1):
35963580
"""
35973581
)
35983582

3599-
def test_setup_exception(self, testdir, flavor):
3583+
def test_setup_exception(self, testdir: Testdir) -> None:
36003584
testdir.makepyfile(
36013585
"""
3602-
from test_context import fixture
3603-
@fixture(scope="module")
3586+
import pytest
3587+
@pytest.fixture(scope="module")
36043588
def arg1():
36053589
pytest.fail("setup")
36063590
yield 1
@@ -3616,11 +3600,11 @@ def test_1(arg1):
36163600
"""
36173601
)
36183602

3619-
def test_teardown_exception(self, testdir, flavor):
3603+
def test_teardown_exception(self, testdir: Testdir) -> None:
36203604
testdir.makepyfile(
36213605
"""
3622-
from test_context import fixture
3623-
@fixture(scope="module")
3606+
import pytest
3607+
@pytest.fixture(scope="module")
36243608
def arg1():
36253609
yield 1
36263610
pytest.fail("teardown")
@@ -3636,11 +3620,11 @@ def test_1(arg1):
36363620
"""
36373621
)
36383622

3639-
def test_yields_more_than_one(self, testdir, flavor):
3623+
def test_yields_more_than_one(self, testdir: Testdir) -> None:
36403624
testdir.makepyfile(
36413625
"""
3642-
from test_context import fixture
3643-
@fixture(scope="module")
3626+
import pytest
3627+
@pytest.fixture(scope="module")
36443628
def arg1():
36453629
yield 1
36463630
yield 2
@@ -3656,11 +3640,11 @@ def test_1(arg1):
36563640
"""
36573641
)
36583642

3659-
def test_custom_name(self, testdir, flavor):
3643+
def test_custom_name(self, testdir: Testdir) -> None:
36603644
testdir.makepyfile(
36613645
"""
3662-
from test_context import fixture
3663-
@fixture(name='meow')
3646+
import pytest
3647+
@pytest.fixture(name='meow')
36643648
def arg1():
36653649
return 'mew'
36663650
def test_1(meow):

testing/test_unittest.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import pytest
66
from _pytest.config import ExitCode
7+
from _pytest.pytester import Testdir
78

89

910
def test_simple_unittest(testdir):
@@ -781,20 +782,18 @@ def test_passing_test_is_fail(self):
781782
assert result.ret == 1
782783

783784

784-
@pytest.mark.parametrize(
785-
"fix_type, stmt", [("fixture", "return"), ("yield_fixture", "yield")]
786-
)
787-
def test_unittest_setup_interaction(testdir, fix_type, stmt):
785+
@pytest.mark.parametrize("stmt", ["return", "yield"])
786+
def test_unittest_setup_interaction(testdir: Testdir, stmt: str) -> None:
788787
testdir.makepyfile(
789788
"""
790789
import unittest
791790
import pytest
792791
class MyTestCase(unittest.TestCase):
793-
@pytest.{fix_type}(scope="class", autouse=True)
792+
@pytest.fixture(scope="class", autouse=True)
794793
def perclass(self, request):
795794
request.cls.hello = "world"
796795
{stmt}
797-
@pytest.{fix_type}(scope="function", autouse=True)
796+
@pytest.fixture(scope="function", autouse=True)
798797
def perfunction(self, request):
799798
request.instance.funcname = request.function.__name__
800799
{stmt}
@@ -809,7 +808,7 @@ def test_method2(self):
809808
def test_classattr(self):
810809
assert self.__class__.hello == "world"
811810
""".format(
812-
fix_type=fix_type, stmt=stmt
811+
stmt=stmt
813812
)
814813
)
815814
result = testdir.runpytest()

0 commit comments

Comments
 (0)