From 89abdb9cc5c0e632618b33477affd68d8ef25e24 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Fri, 31 Jan 2020 19:04:48 +0100 Subject: [PATCH] keep test, warning --- src/_pytest/deprecated.py | 6 ++++++ src/_pytest/nodes.py | 1 + testing/deprecated_test.py | 17 +++++++++++++++++ 3 files changed, 24 insertions(+) diff --git a/src/_pytest/deprecated.py b/src/_pytest/deprecated.py index 09861be6442..69edaf4bef9 100644 --- a/src/_pytest/deprecated.py +++ b/src/_pytest/deprecated.py @@ -9,6 +9,7 @@ in case of warnings which need to format their messages. """ from _pytest.warning_types import PytestDeprecationWarning +from _pytest.warning_types import UnformattedWarning # set of plugins which have been integrated into the core; we use this list to ignore # them during registration to avoid conflicts @@ -35,6 +36,11 @@ "as a keyword argument instead." ) +NODE_USE_FROM_PARENT = UnformattedWarning( + PytestDeprecationWarning, + "direct construction of {name} has been deprecated, please use {name}.from_parent", +) + JUNIT_XML_DEFAULT_FAMILY = PytestDeprecationWarning( "The 'junit_family' default value will change to 'xunit2' in pytest 6.0.\n" "Add 'junit_family=xunit1' to your pytest.ini file to keep the current format " diff --git a/src/_pytest/nodes.py b/src/_pytest/nodes.py index 0ccc8c145cb..32a46b2a457 100644 --- a/src/_pytest/nodes.py +++ b/src/_pytest/nodes.py @@ -19,6 +19,7 @@ from _pytest.compat import getfslineno from _pytest.compat import TYPE_CHECKING from _pytest.config import Config +from _pytest.deprecated import NODE_USE_FROM_PARENT from _pytest.fixtures import FixtureDef from _pytest.fixtures import FixtureLookupError from _pytest.fixtures import FixtureLookupErrorRepr diff --git a/testing/deprecated_test.py b/testing/deprecated_test.py index 5390d038d8b..59cb69a0034 100644 --- a/testing/deprecated_test.py +++ b/testing/deprecated_test.py @@ -1,5 +1,8 @@ +import inspect + import pytest from _pytest import deprecated +from _pytest import nodes @pytest.mark.filterwarnings("default") @@ -73,3 +76,17 @@ def test_foo(): result.stdout.no_fnmatch_line(warning_msg) else: result.stdout.fnmatch_lines([warning_msg]) + + +def test_node_direct_ctor_warning(): + class MockConfig: + pass + + ms = MockConfig() + with pytest.warns( + DeprecationWarning, + match="direct construction of .* has been deprecated, please use .*.from_parent", + ) as w: + nodes.Node(name="test", config=ms, session=ms, nodeid="None") + assert w[0].lineno == inspect.currentframe().f_lineno - 1 + assert w[0].filename == __file__