2020-10-06 22:48:34 +08:00
|
|
|
import re
|
2021-04-03 20:51:36 +08:00
|
|
|
import sys
|
2020-08-15 16:35:54 +08:00
|
|
|
import warnings
|
2020-04-17 21:05:43 +08:00
|
|
|
from unittest import mock
|
2019-10-17 03:52:04 +08:00
|
|
|
|
2016-07-23 22:56:04 +08:00
|
|
|
import pytest
|
2019-06-13 05:49:51 +08:00
|
|
|
from _pytest import deprecated
|
2021-03-21 06:39:38 +08:00
|
|
|
from _pytest.compat import legacy_path
|
2020-11-06 16:48:20 +08:00
|
|
|
from _pytest.pytester import Pytester
|
2021-03-21 06:39:38 +08:00
|
|
|
from pytest import PytestDeprecationWarning
|
2016-07-23 22:56:04 +08:00
|
|
|
|
|
|
|
|
2020-03-30 02:30:16 +08:00
|
|
|
@pytest.mark.parametrize("attribute", pytest.collect.__all__) # type: ignore
|
|
|
|
# false positive due to dynamic attribute
|
2020-12-16 12:16:05 +08:00
|
|
|
def test_pytest_collect_module_deprecated(attribute) -> None:
|
2020-03-30 02:30:16 +08:00
|
|
|
with pytest.warns(DeprecationWarning, match=attribute):
|
|
|
|
getattr(pytest.collect, attribute)
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
2020-07-23 08:36:51 +08:00
|
|
|
def test_fillfuncargs_is_deprecated() -> None:
|
2020-04-17 21:05:43 +08:00
|
|
|
with pytest.warns(
|
|
|
|
pytest.PytestDeprecationWarning,
|
2020-10-06 22:48:34 +08:00
|
|
|
match=re.escape(
|
|
|
|
"pytest._fillfuncargs() is deprecated, use "
|
|
|
|
"function._request._fillfixtures() instead if you cannot avoid reaching into internals."
|
|
|
|
),
|
2020-04-17 21:05:43 +08:00
|
|
|
):
|
|
|
|
pytest._fillfuncargs(mock.Mock())
|
2020-05-11 20:44:01 +08:00
|
|
|
|
|
|
|
|
2020-10-06 22:48:34 +08:00
|
|
|
def test_fillfixtures_is_deprecated() -> None:
|
|
|
|
import _pytest.fixtures
|
|
|
|
|
|
|
|
with pytest.warns(
|
|
|
|
pytest.PytestDeprecationWarning,
|
|
|
|
match=re.escape(
|
|
|
|
"_pytest.fixtures.fillfixtures() is deprecated, use "
|
|
|
|
"function._request._fillfixtures() instead if you cannot avoid reaching into internals."
|
|
|
|
),
|
|
|
|
):
|
|
|
|
_pytest.fixtures.fillfixtures(mock.Mock())
|
|
|
|
|
|
|
|
|
2020-12-16 12:16:05 +08:00
|
|
|
def test_minus_k_dash_is_deprecated(pytester: Pytester) -> None:
|
|
|
|
threepass = pytester.makepyfile(
|
2020-05-11 20:44:01 +08:00
|
|
|
test_threepass="""
|
|
|
|
def test_one(): assert 1
|
|
|
|
def test_two(): assert 1
|
|
|
|
def test_three(): assert 1
|
|
|
|
"""
|
|
|
|
)
|
2020-12-16 12:16:05 +08:00
|
|
|
result = pytester.runpytest("-k=-test_two", threepass)
|
2020-05-11 20:44:01 +08:00
|
|
|
result.stdout.fnmatch_lines(["*The `-k '-expr'` syntax*deprecated*"])
|
|
|
|
|
|
|
|
|
2020-12-16 12:16:05 +08:00
|
|
|
def test_minus_k_colon_is_deprecated(pytester: Pytester) -> None:
|
|
|
|
threepass = pytester.makepyfile(
|
2020-05-11 20:44:01 +08:00
|
|
|
test_threepass="""
|
|
|
|
def test_one(): assert 1
|
|
|
|
def test_two(): assert 1
|
|
|
|
def test_three(): assert 1
|
|
|
|
"""
|
|
|
|
)
|
2020-12-16 12:16:05 +08:00
|
|
|
result = pytester.runpytest("-k", "test_two:", threepass)
|
2020-05-11 20:44:01 +08:00
|
|
|
result.stdout.fnmatch_lines(["*The `-k 'expr:'` syntax*deprecated*"])
|
2020-08-15 16:35:54 +08:00
|
|
|
|
|
|
|
|
2020-12-16 12:16:05 +08:00
|
|
|
def test_fscollector_gethookproxy_isinitpath(pytester: Pytester) -> None:
|
|
|
|
module = pytester.getmodulecol(
|
2020-08-15 16:35:54 +08:00
|
|
|
"""
|
|
|
|
def test_foo(): pass
|
|
|
|
""",
|
|
|
|
withinit=True,
|
|
|
|
)
|
|
|
|
assert isinstance(module, pytest.Module)
|
|
|
|
package = module.parent
|
|
|
|
assert isinstance(package, pytest.Package)
|
|
|
|
|
|
|
|
with pytest.warns(pytest.PytestDeprecationWarning, match="gethookproxy"):
|
2020-12-16 12:16:05 +08:00
|
|
|
package.gethookproxy(pytester.path)
|
2020-08-15 16:35:54 +08:00
|
|
|
|
|
|
|
with pytest.warns(pytest.PytestDeprecationWarning, match="isinitpath"):
|
2020-12-16 12:16:05 +08:00
|
|
|
package.isinitpath(pytester.path)
|
2020-08-15 16:35:54 +08:00
|
|
|
|
|
|
|
# The methods on Session are *not* deprecated.
|
|
|
|
session = module.session
|
|
|
|
with warnings.catch_warnings(record=True) as rec:
|
2020-12-16 12:16:05 +08:00
|
|
|
session.gethookproxy(pytester.path)
|
|
|
|
session.isinitpath(pytester.path)
|
2020-08-15 16:35:54 +08:00
|
|
|
assert len(rec) == 0
|
2020-11-06 16:48:20 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_strict_option_is_deprecated(pytester: Pytester) -> None:
|
|
|
|
"""--strict is a deprecated alias to --strict-markers (#7530)."""
|
|
|
|
pytester.makepyfile(
|
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.mark.unknown
|
|
|
|
def test_foo(): pass
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
result = pytester.runpytest("--strict")
|
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"'unknown' not found in `markers` configuration option",
|
|
|
|
"*PytestDeprecationWarning: The --strict option is deprecated, use --strict-markers instead.",
|
|
|
|
]
|
|
|
|
)
|
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
|
|
|
|
|
|
|
|
|
|
|
def test_raising_unittest_skiptest_during_collection_is_deprecated(
|
|
|
|
pytester: Pytester,
|
|
|
|
) -> None:
|
|
|
|
pytester.makepyfile(
|
|
|
|
"""
|
|
|
|
import unittest
|
|
|
|
raise unittest.SkipTest()
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
result = pytester.runpytest()
|
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"*PytestDeprecationWarning: Raising unittest.SkipTest*",
|
|
|
|
]
|
|
|
|
)
|
2021-03-21 06:39:38 +08:00
|
|
|
|
|
|
|
|
2021-04-03 20:51:36 +08:00
|
|
|
@pytest.mark.parametrize("hooktype", ["hook", "ihook"])
|
|
|
|
def test_hookproxy_warnings_for_fspath(tmp_path, hooktype, request):
|
2021-03-21 06:39:38 +08:00
|
|
|
path = legacy_path(tmp_path)
|
|
|
|
|
2021-04-02 06:58:05 +08:00
|
|
|
PATH_WARN_MATCH = r".*path: py\.path\.local\) argument is deprecated, please use \(fspath: pathlib\.Path.*"
|
2021-04-03 20:51:36 +08:00
|
|
|
if hooktype == "ihook":
|
|
|
|
hooks = request.node.ihook
|
|
|
|
else:
|
|
|
|
hooks = request.config.hook
|
2021-04-02 06:58:05 +08:00
|
|
|
|
|
|
|
with pytest.warns(PytestDeprecationWarning, match=PATH_WARN_MATCH) as r:
|
2021-04-03 20:51:36 +08:00
|
|
|
l1 = sys._getframe().f_lineno
|
|
|
|
hooks.pytest_ignore_collect(config=request.config, path=path, fspath=tmp_path)
|
|
|
|
l2 = sys._getframe().f_lineno
|
2021-04-02 06:58:05 +08:00
|
|
|
|
|
|
|
(record,) = r
|
|
|
|
assert record.filename == __file__
|
2021-04-03 20:51:36 +08:00
|
|
|
assert l1 < record.lineno < l2
|
|
|
|
|
|
|
|
hooks.pytest_ignore_collect(config=request.config, fspath=tmp_path)
|
2021-05-15 22:15:43 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_warns_none_is_deprecated():
|
|
|
|
with pytest.warns(
|
|
|
|
PytestDeprecationWarning,
|
2021-05-17 16:23:08 +08:00
|
|
|
match=re.escape(
|
|
|
|
"Passing None to catch any warning has been deprecated, pass no arguments instead:\n "
|
|
|
|
"Replace pytest.warns(None) by simply pytest.warns()."
|
|
|
|
),
|
2021-05-15 22:15:43 +08:00
|
|
|
):
|
2021-05-17 16:50:59 +08:00
|
|
|
with pytest.warns(None): # type: ignore[call-overload]
|
2021-05-16 19:07:39 +08:00
|
|
|
pass
|