diff --git a/src/_pytest/config/compat.py b/src/_pytest/config/compat.py index d9e6e280c..c93f3738d 100644 --- a/src/_pytest/config/compat.py +++ b/src/_pytest/config/compat.py @@ -49,7 +49,8 @@ class PathAwareHookProxy: warnings.warn( HOOK_LEGACY_PATH_ARG.format( pylib_path_arg=fspath_var, pathlib_path_arg=path_var - ) + ), + stacklevel=2, ) path_value, fspath_value = _imply_path(path_value, fspath_value) kw[path_var] = path_value diff --git a/testing/deprecated_test.py b/testing/deprecated_test.py index b4e73dd5b..0c84ab422 100644 --- a/testing/deprecated_test.py +++ b/testing/deprecated_test.py @@ -160,20 +160,22 @@ def test_raising_unittest_skiptest_during_collection_is_deprecated( def test_hookproxy_warnings_for_fspath(pytestconfig, tmp_path, request): path = legacy_path(tmp_path) - with pytest.warns( - PytestDeprecationWarning, - match="path : py.path.local is deprecated, please use fspath : pathlib.Path", - ): + PATH_WARN_MATCH = r".*path: py\.path\.local\) argument is deprecated, please use \(fspath: pathlib\.Path.*" + + with pytest.warns(PytestDeprecationWarning, match=PATH_WARN_MATCH) as r: pytestconfig.hook.pytest_ignore_collect( config=pytestconfig, path=path, fspath=tmp_path ) - with pytest.warns( - PytestDeprecationWarning, - match="path : py.path.local is deprecated, please use fspath : pathlib.Path", - ): + (record,) = r + assert record.filename == __file__ + assert record.lineno == 166 + + with pytest.warns(PytestDeprecationWarning, match=PATH_WARN_MATCH) as r: request.node.ihook.pytest_ignore_collect( config=pytestconfig, path=path, fspath=tmp_path ) - + (record,) = r + assert record.filename == __file__ + assert record.lineno == 174 pytestconfig.hook.pytest_ignore_collect(config=pytestconfig, fspath=tmp_path) request.node.ihook.pytest_ignore_collect(config=pytestconfig, fspath=tmp_path)