fixtures: deprecate pytest.yield_fixture()
This commit is contained in:
parent
3bcd316f07
commit
4c0513bc18
|
@ -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.
|
|
@ -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).
|
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
|
The ``pytest_warning_captured`` hook
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
|
@ -32,6 +32,10 @@ PYTEST_COLLECT_MODULE = UnformattedWarning(
|
||||||
"Please update to the new name.",
|
"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(
|
MINUS_K_DASH = PytestDeprecationWarning(
|
||||||
"The `-k '-expr'` syntax to -k is deprecated.\nUse `-k 'not expr'` instead."
|
"The `-k '-expr'` syntax to -k is deprecated.\nUse `-k 'not expr'` instead."
|
||||||
|
|
|
@ -50,6 +50,7 @@ from _pytest.config import _PluggyPlugin
|
||||||
from _pytest.config import Config
|
from _pytest.config import Config
|
||||||
from _pytest.config.argparsing import Parser
|
from _pytest.config.argparsing import Parser
|
||||||
from _pytest.deprecated import FILLFUNCARGS
|
from _pytest.deprecated import FILLFUNCARGS
|
||||||
|
from _pytest.deprecated import YIELD_FIXTURE
|
||||||
from _pytest.mark import Mark
|
from _pytest.mark import Mark
|
||||||
from _pytest.mark import ParameterSet
|
from _pytest.mark import ParameterSet
|
||||||
from _pytest.mark.structures import MarkDecorator
|
from _pytest.mark.structures import MarkDecorator
|
||||||
|
@ -1339,6 +1340,7 @@ def yield_fixture(
|
||||||
.. deprecated:: 3.0
|
.. deprecated:: 3.0
|
||||||
Use :py:func:`pytest.fixture` directly instead.
|
Use :py:func:`pytest.fixture` directly instead.
|
||||||
"""
|
"""
|
||||||
|
warnings.warn(YIELD_FIXTURE, stacklevel=2)
|
||||||
return fixture(
|
return fixture(
|
||||||
fixture_function,
|
fixture_function,
|
||||||
*args,
|
*args,
|
||||||
|
|
|
@ -115,3 +115,11 @@ def test_strict_option_is_deprecated(pytester: Pytester) -> None:
|
||||||
"*PytestDeprecationWarning: The --strict option is deprecated, use --strict-markers instead.",
|
"*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
|
||||||
|
|
|
@ -8,6 +8,7 @@ from _pytest.compat import getfuncargnames
|
||||||
from _pytest.config import ExitCode
|
from _pytest.config import ExitCode
|
||||||
from _pytest.fixtures import FixtureRequest
|
from _pytest.fixtures import FixtureRequest
|
||||||
from _pytest.pytester import get_public_names
|
from _pytest.pytester import get_public_names
|
||||||
|
from _pytest.pytester import Testdir
|
||||||
|
|
||||||
|
|
||||||
def test_getfuncargnames_functions():
|
def test_getfuncargnames_functions():
|
||||||
|
@ -3526,28 +3527,11 @@ class TestShowFixtures:
|
||||||
|
|
||||||
|
|
||||||
class TestContextManagerFixtureFuncs:
|
class TestContextManagerFixtureFuncs:
|
||||||
@pytest.fixture(params=["fixture", "yield_fixture"])
|
def test_simple(self, testdir: Testdir) -> None:
|
||||||
def flavor(self, request, testdir, monkeypatch):
|
|
||||||
monkeypatch.setenv("PYTEST_FIXTURE_FLAVOR", request.param)
|
|
||||||
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(
|
testdir.makepyfile(
|
||||||
"""
|
"""
|
||||||
from test_context import fixture
|
import pytest
|
||||||
@fixture
|
@pytest.fixture
|
||||||
def arg1():
|
def arg1():
|
||||||
print("setup")
|
print("setup")
|
||||||
yield 1
|
yield 1
|
||||||
|
@ -3571,11 +3555,11 @@ class TestContextManagerFixtureFuncs:
|
||||||
"""
|
"""
|
||||||
)
|
)
|
||||||
|
|
||||||
def test_scoped(self, testdir, flavor):
|
def test_scoped(self, testdir: Testdir) -> None:
|
||||||
testdir.makepyfile(
|
testdir.makepyfile(
|
||||||
"""
|
"""
|
||||||
from test_context import fixture
|
import pytest
|
||||||
@fixture(scope="module")
|
@pytest.fixture(scope="module")
|
||||||
def arg1():
|
def arg1():
|
||||||
print("setup")
|
print("setup")
|
||||||
yield 1
|
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(
|
testdir.makepyfile(
|
||||||
"""
|
"""
|
||||||
from test_context import fixture
|
import pytest
|
||||||
@fixture(scope="module")
|
@pytest.fixture(scope="module")
|
||||||
def arg1():
|
def arg1():
|
||||||
pytest.fail("setup")
|
pytest.fail("setup")
|
||||||
yield 1
|
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(
|
testdir.makepyfile(
|
||||||
"""
|
"""
|
||||||
from test_context import fixture
|
import pytest
|
||||||
@fixture(scope="module")
|
@pytest.fixture(scope="module")
|
||||||
def arg1():
|
def arg1():
|
||||||
yield 1
|
yield 1
|
||||||
pytest.fail("teardown")
|
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(
|
testdir.makepyfile(
|
||||||
"""
|
"""
|
||||||
from test_context import fixture
|
import pytest
|
||||||
@fixture(scope="module")
|
@pytest.fixture(scope="module")
|
||||||
def arg1():
|
def arg1():
|
||||||
yield 1
|
yield 1
|
||||||
yield 2
|
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(
|
testdir.makepyfile(
|
||||||
"""
|
"""
|
||||||
from test_context import fixture
|
import pytest
|
||||||
@fixture(name='meow')
|
@pytest.fixture(name='meow')
|
||||||
def arg1():
|
def arg1():
|
||||||
return 'mew'
|
return 'mew'
|
||||||
def test_1(meow):
|
def test_1(meow):
|
||||||
|
|
|
@ -4,6 +4,7 @@ from typing import List
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
from _pytest.config import ExitCode
|
from _pytest.config import ExitCode
|
||||||
|
from _pytest.pytester import Testdir
|
||||||
|
|
||||||
|
|
||||||
def test_simple_unittest(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
|
assert result.ret == 1
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize("stmt", ["return", "yield"])
|
||||||
"fix_type, stmt", [("fixture", "return"), ("yield_fixture", "yield")]
|
def test_unittest_setup_interaction(testdir: Testdir, stmt: str) -> None:
|
||||||
)
|
|
||||||
def test_unittest_setup_interaction(testdir, fix_type, stmt):
|
|
||||||
testdir.makepyfile(
|
testdir.makepyfile(
|
||||||
"""
|
"""
|
||||||
import unittest
|
import unittest
|
||||||
import pytest
|
import pytest
|
||||||
class MyTestCase(unittest.TestCase):
|
class MyTestCase(unittest.TestCase):
|
||||||
@pytest.{fix_type}(scope="class", autouse=True)
|
@pytest.fixture(scope="class", autouse=True)
|
||||||
def perclass(self, request):
|
def perclass(self, request):
|
||||||
request.cls.hello = "world"
|
request.cls.hello = "world"
|
||||||
{stmt}
|
{stmt}
|
||||||
@pytest.{fix_type}(scope="function", autouse=True)
|
@pytest.fixture(scope="function", autouse=True)
|
||||||
def perfunction(self, request):
|
def perfunction(self, request):
|
||||||
request.instance.funcname = request.function.__name__
|
request.instance.funcname = request.function.__name__
|
||||||
{stmt}
|
{stmt}
|
||||||
|
@ -809,7 +808,7 @@ def test_unittest_setup_interaction(testdir, fix_type, stmt):
|
||||||
def test_classattr(self):
|
def test_classattr(self):
|
||||||
assert self.__class__.hello == "world"
|
assert self.__class__.hello == "world"
|
||||||
""".format(
|
""".format(
|
||||||
fix_type=fix_type, stmt=stmt
|
stmt=stmt
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
result = testdir.runpytest()
|
result = testdir.runpytest()
|
||||||
|
|
Loading…
Reference in New Issue