From 24306f23abfac736f3a26e32fff344f96eb628af Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Thu, 15 Sep 2022 08:18:49 -0300 Subject: [PATCH] Move test to deprecated_test and improve message --- src/_pytest/deprecated.py | 2 +- src/_pytest/nose.py | 6 +++++- testing/deprecated_test.py | 28 ++++++++++++++++++++++++++++ testing/test_nose.py | 25 ------------------------- 4 files changed, 34 insertions(+), 27 deletions(-) diff --git a/src/_pytest/deprecated.py b/src/_pytest/deprecated.py index 8f5f393eb..38af4b24c 100644 --- a/src/_pytest/deprecated.py +++ b/src/_pytest/deprecated.py @@ -25,7 +25,7 @@ DEPRECATED_EXTERNAL_PLUGINS = { NOSE_SUPPORT = UnformattedWarning( PytestRemovedIn8Warning, "Support for nose tests is deprecated and will be removed in a future release.\n" - "{nodeid} is using nose method: `{nose_method}`", + "{nodeid} is using nose method: `{method}` ({stage})", ) diff --git a/src/_pytest/nose.py b/src/_pytest/nose.py index 422138e85..273bd045f 100644 --- a/src/_pytest/nose.py +++ b/src/_pytest/nose.py @@ -39,8 +39,12 @@ def call_optional(obj: object, name: str, nodeid: str) -> bool: return False if not callable(method): return False + # Warn about deprecation of this plugin. + method_name = getattr(method, "__name__", str(method)) + warnings.warn( + NOSE_SUPPORT.format(nodeid=nodeid, method=method_name, stage=name), stacklevel=2 + ) # If there are any problems allow the exception to raise rather than # silently ignoring it. - warnings.warn(NOSE_SUPPORT.format(nodeid=nodeid, nose_method=name), stacklevel=2) method() return True diff --git a/testing/deprecated_test.py b/testing/deprecated_test.py index db649841a..dcc397f8a 100644 --- a/testing/deprecated_test.py +++ b/testing/deprecated_test.py @@ -231,3 +231,31 @@ def test_importing_instance_is_deprecated(pytester: Pytester) -> None: match=re.escape("The pytest.Instance collector type is deprecated"), ): from _pytest.python import Instance # noqa: F401 + + +def test_nose_deprecated(pytester: Pytester) -> None: + pytest.importorskip("nose") + pytester.makepyfile( + """ + from nose.tools import with_setup + + def setup_fn_no_op(): + ... + + def teardown_fn_no_op(): + ... + + @with_setup(setup_fn_no_op, teardown_fn_no_op) + def test_omits_warnings(): + ... + """ + ) + output = pytester.runpytest() + message = [ + "*PytestRemovedIn8Warning: Support for nose tests is deprecated and will be removed in a future release.", + "*test_nose_deprecated.py::test_omits_warnings is using nose method: `setup_fn_no_op` (setup)", + "*PytestRemovedIn8Warning: Support for nose tests is deprecated and will be removed in a future release.", + "*test_nose_deprecated.py::test_omits_warnings is using nose method: `teardown_fn_no_op` (teardown)", + ] + output.stdout.fnmatch_lines(message) + output.assert_outcomes(passed=1) diff --git a/testing/test_nose.py b/testing/test_nose.py index 99eba03b6..92d6b95fd 100644 --- a/testing/test_nose.py +++ b/testing/test_nose.py @@ -496,28 +496,3 @@ def test_nose_setup_skipped_if_non_callable(pytester: Pytester) -> None: ) result = pytester.runpytest(p, "-p", "nose") assert result.ret == 0 - - -def test_nose_setup_and_teardown_is_deprecated(pytester: Pytester) -> None: - pytester.makepyfile( - """ - from nose.tools import with_setup - - def setup_fn_no_op(): - ... - - def teardown_fn_no_op(): - ... - - @with_setup(setup_fn_no_op, teardown_fn_no_op) - def test_omits_warnings(): - ... - """ - ) - output = pytester.runpytest() - message = [ - "*PytestRemovedIn8Warning: Support for nose tests is deprecated and will be removed in a future release.", - "*test_nose_setup_and_teardown_is_deprecated.py::test_omits_warnings is using nose method: *", - ] - output.stdout.fnmatch_lines(message) - output.assert_outcomes(passed=1, warnings=2)