Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixtures: deprecate pytest.yield_fixture() #7988

Merged
merged 1 commit into from Nov 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions 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.
9 changes: 9 additions & 0 deletions doc/en/deprecations.rst
Expand Up @@ -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
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down
4 changes: 4 additions & 0 deletions src/_pytest/deprecated.py
Expand Up @@ -32,6 +32,10 @@
"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."
Expand Down
2 changes: 2 additions & 0 deletions src/_pytest/fixtures.py
Expand Up @@ -50,6 +50,7 @@
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
Expand Down Expand Up @@ -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,
Expand Down
8 changes: 8 additions & 0 deletions testing/deprecated_test.py
Expand Up @@ -115,3 +115,11 @@ def test_foo(): pass
"*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
54 changes: 19 additions & 35 deletions testing/python/fixtures.py
Expand Up @@ -8,6 +8,7 @@
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():
Expand Down Expand Up @@ -3526,28 +3527,11 @@ def foo():


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
Expand All @@ -3571,11 +3555,11 @@ def test_2(arg1):
"""
)

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
Expand All @@ -3596,11 +3580,11 @@ def test_2(arg1):
"""
)

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
Expand All @@ -3616,11 +3600,11 @@ def test_1(arg1):
"""
)

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")
Expand All @@ -3636,11 +3620,11 @@ def test_1(arg1):
"""
)

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
Expand All @@ -3656,11 +3640,11 @@ def test_1(arg1):
"""
)

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):
Expand Down
13 changes: 6 additions & 7 deletions testing/test_unittest.py
Expand Up @@ -4,6 +4,7 @@

import pytest
from _pytest.config import ExitCode
from _pytest.pytester import Testdir


def test_simple_unittest(testdir):
Expand Down Expand Up @@ -781,20 +782,18 @@ def test_passing_test_is_fail(self):
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}
Expand All @@ -809,7 +808,7 @@ def test_method2(self):
def test_classattr(self):
assert self.__class__.hello == "world"
""".format(
fix_type=fix_type, stmt=stmt
stmt=stmt
)
)
result = testdir.runpytest()
Expand Down