2024-01-28 21:12:42 +08:00
|
|
|
# mypy: allow-untyped-defs
|
2019-06-13 05:49:51 +08:00
|
|
|
from _pytest import deprecated
|
2020-11-06 16:48:20 +08:00
|
|
|
from _pytest.pytester import Pytester
|
2021-03-21 06:39:38 +08:00
|
|
|
import pytest
|
|
|
|
from pytest import PytestDeprecationWarning
|
2016-07-23 22:56:04 +08:00
|
|
|
|
|
|
|
|
2019-07-06 06:04:55 +08:00
|
|
|
@pytest.mark.parametrize("plugin", sorted(deprecated.DEPRECATED_EXTERNAL_PLUGINS))
|
2018-12-12 06:02:36 +08:00
|
|
|
@pytest.mark.filterwarnings("default")
|
2020-12-16 12:16:05 +08:00
|
|
|
def test_external_plugins_integrated(pytester: Pytester, plugin) -> None:
|
|
|
|
pytester.syspathinsert()
|
|
|
|
pytester.makepyfile(**{plugin: ""})
|
2019-06-13 05:49:51 +08:00
|
|
|
|
|
|
|
with pytest.warns(pytest.PytestConfigWarning):
|
2020-12-16 12:16:05 +08:00
|
|
|
pytester.parseconfig("-p", plugin)
|
2019-11-14 05:20:44 +08:00
|
|
|
|
|
|
|
|
2021-10-03 22:01:40 +08:00
|
|
|
def test_hookspec_via_function_attributes_are_deprecated():
|
|
|
|
from _pytest.config import PytestPluginManager
|
|
|
|
|
|
|
|
pm = PytestPluginManager()
|
|
|
|
|
|
|
|
class DeprecatedHookMarkerSpec:
|
|
|
|
def pytest_bad_hook(self):
|
|
|
|
pass
|
|
|
|
|
2022-07-31 23:11:34 +08:00
|
|
|
pytest_bad_hook.historic = False # type: ignore[attr-defined]
|
2021-10-03 22:01:40 +08:00
|
|
|
|
|
|
|
with pytest.warns(
|
|
|
|
PytestDeprecationWarning,
|
2022-07-31 23:11:34 +08:00
|
|
|
match=r"Please use the pytest\.hookspec\(historic=False\) decorator",
|
2021-10-03 22:01:40 +08:00
|
|
|
) as recorder:
|
|
|
|
pm.add_hookspecs(DeprecatedHookMarkerSpec)
|
|
|
|
(record,) = recorder
|
|
|
|
assert (
|
|
|
|
record.lineno
|
|
|
|
== DeprecatedHookMarkerSpec.pytest_bad_hook.__code__.co_firstlineno
|
|
|
|
)
|
|
|
|
assert record.filename == __file__
|
|
|
|
|
|
|
|
|
|
|
|
def test_hookimpl_via_function_attributes_are_deprecated():
|
|
|
|
from _pytest.config import PytestPluginManager
|
|
|
|
|
|
|
|
pm = PytestPluginManager()
|
|
|
|
|
|
|
|
class DeprecatedMarkImplPlugin:
|
|
|
|
def pytest_runtest_call(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
pytest_runtest_call.tryfirst = True # type: ignore[attr-defined]
|
|
|
|
|
|
|
|
with pytest.warns(
|
|
|
|
PytestDeprecationWarning,
|
|
|
|
match=r"Please use the pytest.hookimpl\(tryfirst=True\)",
|
|
|
|
) as recorder:
|
|
|
|
pm.register(DeprecatedMarkImplPlugin())
|
|
|
|
(record,) = recorder
|
|
|
|
assert (
|
|
|
|
record.lineno
|
|
|
|
== DeprecatedMarkImplPlugin.pytest_runtest_call.__code__.co_firstlineno
|
|
|
|
)
|
|
|
|
assert record.filename == __file__
|
|
|
|
|
|
|
|
|
2020-11-01 04:44:10 +08:00
|
|
|
def test_yield_fixture_is_deprecated() -> None:
|
|
|
|
with pytest.warns(DeprecationWarning, match=r"yield_fixture is deprecated"):
|
|
|
|
|
|
|
|
@pytest.yield_fixture
|
|
|
|
def fix():
|
|
|
|
assert False
|
2020-09-28 03:20:31 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_private_is_deprecated() -> None:
|
|
|
|
class PrivateInit:
|
|
|
|
def __init__(self, foo: int, *, _ispytest: bool = False) -> None:
|
|
|
|
deprecated.check_ispytest(_ispytest)
|
|
|
|
|
|
|
|
with pytest.warns(
|
|
|
|
pytest.PytestDeprecationWarning, match="private pytest class or function"
|
|
|
|
):
|
|
|
|
PrivateInit(10)
|
|
|
|
|
|
|
|
# Doesn't warn.
|
|
|
|
PrivateInit(10, _ispytest=True)
|
2021-01-15 00:14:39 +08:00
|
|
|
|
|
|
|
|
2022-10-10 20:48:26 +08:00
|
|
|
def test_fixture_disallow_on_marked_functions():
|
|
|
|
"""Test that applying @pytest.fixture to a marked function warns (#3364)."""
|
|
|
|
with pytest.warns(
|
2023-12-31 04:12:45 +08:00
|
|
|
pytest.PytestRemovedIn9Warning,
|
2022-10-10 20:48:26 +08:00
|
|
|
match=r"Marks applied to fixtures have no effect",
|
|
|
|
) as record:
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
@pytest.mark.parametrize("example", ["hello"])
|
|
|
|
@pytest.mark.usefixtures("tmp_path")
|
|
|
|
def foo():
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
2022-10-10 20:52:26 +08:00
|
|
|
# it's only possible to get one warning here because you're already prevented
|
|
|
|
# from applying @fixture twice
|
|
|
|
# ValueError("fixture is being applied more than once to the same function")
|
2022-10-10 20:48:26 +08:00
|
|
|
assert len(record) == 1
|
|
|
|
|
|
|
|
|
|
|
|
def test_fixture_disallow_marks_on_fixtures():
|
|
|
|
"""Test that applying a mark to a fixture warns (#3364)."""
|
|
|
|
with pytest.warns(
|
2023-12-31 04:12:45 +08:00
|
|
|
pytest.PytestRemovedIn9Warning,
|
2022-10-10 20:48:26 +08:00
|
|
|
match=r"Marks applied to fixtures have no effect",
|
|
|
|
) as record:
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("example", ["hello"])
|
|
|
|
@pytest.mark.usefixtures("tmp_path")
|
|
|
|
@pytest.fixture
|
|
|
|
def foo():
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
2022-10-10 20:52:26 +08:00
|
|
|
assert len(record) == 2 # one for each mark decorator
|
2024-02-23 14:11:05 +08:00
|
|
|
# should point to this file
|
|
|
|
assert all(rec.filename == __file__ for rec in record)
|
2022-10-10 20:52:26 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_fixture_disallowed_between_marks():
|
|
|
|
"""Test that applying a mark to a fixture warns (#3364)."""
|
|
|
|
with pytest.warns(
|
2023-12-31 04:12:45 +08:00
|
|
|
pytest.PytestRemovedIn9Warning,
|
2022-10-10 20:52:26 +08:00
|
|
|
match=r"Marks applied to fixtures have no effect",
|
|
|
|
) as record:
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("example", ["hello"])
|
|
|
|
@pytest.fixture
|
|
|
|
@pytest.mark.usefixtures("tmp_path")
|
|
|
|
def foo():
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
assert len(record) == 2 # one for each mark decorator
|