From 4c0513bc18f52b24b69d60c0b1b5e9666e084c06 Mon Sep 17 00:00:00 2001 From: Ran Benita Date: Sat, 31 Oct 2020 22:44:10 +0200 Subject: [PATCH] fixtures: deprecate pytest.yield_fixture() --- changelog/7988.deprecation.rst | 3 ++ doc/en/deprecations.rst | 9 ++++++ src/_pytest/deprecated.py | 4 +++ src/_pytest/fixtures.py | 2 ++ testing/deprecated_test.py | 8 +++++ testing/python/fixtures.py | 54 ++++++++++++---------------------- testing/test_unittest.py | 13 ++++---- 7 files changed, 51 insertions(+), 42 deletions(-) create mode 100644 changelog/7988.deprecation.rst diff --git a/changelog/7988.deprecation.rst b/changelog/7988.deprecation.rst new file mode 100644 index 000000000..34f646c9a --- /dev/null +++ b/changelog/7988.deprecation.rst @@ -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. diff --git a/doc/en/deprecations.rst b/doc/en/deprecations.rst index d588b1bea..5ef1053e0 100644 --- a/doc/en/deprecations.rst +++ b/doc/en/deprecations.rst @@ -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 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/src/_pytest/deprecated.py b/src/_pytest/deprecated.py index a9a162f41..2e9154e83 100644 --- a/src/_pytest/deprecated.py +++ b/src/_pytest/deprecated.py @@ -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." diff --git a/src/_pytest/fixtures.py b/src/_pytest/fixtures.py index 18094f21c..cef998c03 100644 --- a/src/_pytest/fixtures.py +++ b/src/_pytest/fixtures.py @@ -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, diff --git a/testing/deprecated_test.py b/testing/deprecated_test.py index e0bc1db70..0d1b58ad1 100644 --- a/testing/deprecated_test.py +++ b/testing/deprecated_test.py @@ -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 diff --git a/testing/python/fixtures.py b/testing/python/fixtures.py index a4838ee51..a5637b476 100644 --- a/testing/python/fixtures.py +++ b/testing/python/fixtures.py @@ -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) - 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): + def test_simple(self, testdir: Testdir) -> None: testdir.makepyfile( """ - from test_context import fixture - @fixture + import pytest + @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): diff --git a/testing/test_unittest.py b/testing/test_unittest.py index f6c8c48ed..2c8d03cb9 100644 --- a/testing/test_unittest.py +++ b/testing/test_unittest.py @@ -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()