Merge pull request #7988 from bluetech/deprecate-yield-fixture

fixtures: deprecate pytest.yield_fixture()
This commit is contained in:
Ran Benita 2020-11-07 18:07:00 +02:00 committed by GitHub
commit c784c142a4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 51 additions and 42 deletions

View File

@ -0,0 +1,3 @@
The ``@pytest.yield_fixture`` decorator/function is now deprecated. Use :func:`pytest.fixture` instead.
``yield_fixture`` has been an alias for ``fixture`` for a very long time, so can be search/replaced safely.

View File

@ -31,6 +31,15 @@ flag for all strictness related options (``--strict-markers`` and ``--strict-con
at the moment, more might be introduced in the future).
The ``yield_fixture`` function/decorator
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.. deprecated:: 6.2
``pytest.yield_fixture`` is a deprecated alias for :func:`pytest.fixture`.
It has been so for a very long time, so can be search/replaced safely.
The ``pytest_warning_captured`` hook
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

View File

@ -32,6 +32,10 @@ PYTEST_COLLECT_MODULE = UnformattedWarning(
"Please update to the new name.",
)
YIELD_FIXTURE = PytestDeprecationWarning(
"@pytest.yield_fixture is deprecated.\n"
"Use @pytest.fixture instead; they are the same."
)
MINUS_K_DASH = PytestDeprecationWarning(
"The `-k '-expr'` syntax to -k is deprecated.\nUse `-k 'not expr'` instead."

View File

@ -50,6 +50,7 @@ from _pytest.config import _PluggyPlugin
from _pytest.config import Config
from _pytest.config.argparsing import Parser
from _pytest.deprecated import FILLFUNCARGS
from _pytest.deprecated import YIELD_FIXTURE
from _pytest.mark import Mark
from _pytest.mark import ParameterSet
from _pytest.mark.structures import MarkDecorator
@ -1339,6 +1340,7 @@ def yield_fixture(
.. deprecated:: 3.0
Use :py:func:`pytest.fixture` directly instead.
"""
warnings.warn(YIELD_FIXTURE, stacklevel=2)
return fixture(
fixture_function,
*args,

View File

@ -115,3 +115,11 @@ def test_strict_option_is_deprecated(pytester: Pytester) -> None:
"*PytestDeprecationWarning: The --strict option is deprecated, use --strict-markers instead.",
]
)
def test_yield_fixture_is_deprecated() -> None:
with pytest.warns(DeprecationWarning, match=r"yield_fixture is deprecated"):
@pytest.yield_fixture
def fix():
assert False

View File

@ -8,6 +8,7 @@ from _pytest.compat import getfuncargnames
from _pytest.config import ExitCode
from _pytest.fixtures import FixtureRequest
from _pytest.pytester import get_public_names
from _pytest.pytester import Testdir
def test_getfuncargnames_functions():
@ -3526,28 +3527,11 @@ class TestShowFixtures:
class TestContextManagerFixtureFuncs:
@pytest.fixture(params=["fixture", "yield_fixture"])
def flavor(self, request, testdir, monkeypatch):
monkeypatch.setenv("PYTEST_FIXTURE_FLAVOR", request.param)
def test_simple(self, testdir: Testdir) -> None:
testdir.makepyfile(
test_context="""
import os
"""
import pytest
import warnings
VAR = "PYTEST_FIXTURE_FLAVOR"
if VAR not in os.environ:
warnings.warn("PYTEST_FIXTURE_FLAVOR was not set, assuming fixture")
fixture = pytest.fixture
else:
fixture = getattr(pytest, os.environ[VAR])
"""
)
def test_simple(self, testdir, flavor):
testdir.makepyfile(
"""
from test_context import fixture
@fixture
@pytest.fixture
def arg1():
print("setup")
yield 1
@ -3571,11 +3555,11 @@ class TestContextManagerFixtureFuncs:
"""
)
def test_scoped(self, testdir, flavor):
def test_scoped(self, testdir: Testdir) -> None:
testdir.makepyfile(
"""
from test_context import fixture
@fixture(scope="module")
import pytest
@pytest.fixture(scope="module")
def arg1():
print("setup")
yield 1
@ -3596,11 +3580,11 @@ class TestContextManagerFixtureFuncs:
"""
)
def test_setup_exception(self, testdir, flavor):
def test_setup_exception(self, testdir: Testdir) -> None:
testdir.makepyfile(
"""
from test_context import fixture
@fixture(scope="module")
import pytest
@pytest.fixture(scope="module")
def arg1():
pytest.fail("setup")
yield 1
@ -3616,11 +3600,11 @@ class TestContextManagerFixtureFuncs:
"""
)
def test_teardown_exception(self, testdir, flavor):
def test_teardown_exception(self, testdir: Testdir) -> None:
testdir.makepyfile(
"""
from test_context import fixture
@fixture(scope="module")
import pytest
@pytest.fixture(scope="module")
def arg1():
yield 1
pytest.fail("teardown")
@ -3636,11 +3620,11 @@ class TestContextManagerFixtureFuncs:
"""
)
def test_yields_more_than_one(self, testdir, flavor):
def test_yields_more_than_one(self, testdir: Testdir) -> None:
testdir.makepyfile(
"""
from test_context import fixture
@fixture(scope="module")
import pytest
@pytest.fixture(scope="module")
def arg1():
yield 1
yield 2
@ -3656,11 +3640,11 @@ class TestContextManagerFixtureFuncs:
"""
)
def test_custom_name(self, testdir, flavor):
def test_custom_name(self, testdir: Testdir) -> None:
testdir.makepyfile(
"""
from test_context import fixture
@fixture(name='meow')
import pytest
@pytest.fixture(name='meow')
def arg1():
return 'mew'
def test_1(meow):

View File

@ -4,6 +4,7 @@ from typing import List
import pytest
from _pytest.config import ExitCode
from _pytest.pytester import Testdir
def test_simple_unittest(testdir):
@ -781,20 +782,18 @@ def test_unittest_expected_failure_for_passing_test_is_fail(testdir, runner):
assert result.ret == 1
@pytest.mark.parametrize(
"fix_type, stmt", [("fixture", "return"), ("yield_fixture", "yield")]
)
def test_unittest_setup_interaction(testdir, fix_type, stmt):
@pytest.mark.parametrize("stmt", ["return", "yield"])
def test_unittest_setup_interaction(testdir: Testdir, stmt: str) -> None:
testdir.makepyfile(
"""
import unittest
import pytest
class MyTestCase(unittest.TestCase):
@pytest.{fix_type}(scope="class", autouse=True)
@pytest.fixture(scope="class", autouse=True)
def perclass(self, request):
request.cls.hello = "world"
{stmt}
@pytest.{fix_type}(scope="function", autouse=True)
@pytest.fixture(scope="function", autouse=True)
def perfunction(self, request):
request.instance.funcname = request.function.__name__
{stmt}
@ -809,7 +808,7 @@ def test_unittest_setup_interaction(testdir, fix_type, stmt):
def test_classattr(self):
assert self.__class__.hello == "world"
""".format(
fix_type=fix_type, stmt=stmt
stmt=stmt
)
)
result = testdir.runpytest()