From 8f5cb461a8730501235eae611dda6b5b6edc539c Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Wed, 5 Jun 2019 19:02:52 -0300 Subject: [PATCH] Turn PytestDeprecationWarning into error Fix #5402 --- changelog/5402.removal.rst | 23 +++++++++++++++++++++++ src/_pytest/warnings.py | 1 + testing/python/fixtures.py | 3 +-- testing/test_warnings.py | 31 +++++++++++++++++++++++++++++++ 4 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 changelog/5402.removal.rst diff --git a/changelog/5402.removal.rst b/changelog/5402.removal.rst new file mode 100644 index 000000000..29921dd97 --- /dev/null +++ b/changelog/5402.removal.rst @@ -0,0 +1,23 @@ +**PytestDeprecationWarning are now errors by default.** + +Following our plan to remove deprecated features with as little disruption as +possible, all warnings of type ``PytestDeprecationWarning`` now generate errors +instead of warning messages. + +**The affected features will be effectively removed in pytest 5.1**, so please consult the +`Deprecations and Removals `__ +section in the docs for directions on how to update existing code. + +In the pytest ``5.0.X`` series, it is possible to change the errors back into warnings as a stop +gap measure by adding this to your ``pytest.ini`` file: + +.. code-block:: ini + + [pytest] + filterwarnings = + ignore::pytest.PytestDeprecationWarning + +But this will stop working when pytest ``5.1`` is released. + +**If you have concerns** about the removal of a specific feature, please add a +comment to `#5402 `__. diff --git a/src/_pytest/warnings.py b/src/_pytest/warnings.py index 2f1086225..f47eee0d4 100644 --- a/src/_pytest/warnings.py +++ b/src/_pytest/warnings.py @@ -75,6 +75,7 @@ def catch_warnings_for_item(config, ihook, when, item): warnings.filterwarnings("always", category=PendingDeprecationWarning) warnings.filterwarnings("error", category=pytest.RemovedInPytest4Warning) + warnings.filterwarnings("error", category=pytest.PytestDeprecationWarning) # filters should have this precedence: mark, cmdline options, ini # filters should be applied in the inverse order of precedence diff --git a/testing/python/fixtures.py b/testing/python/fixtures.py index a9ea333ad..75467fb09 100644 --- a/testing/python/fixtures.py +++ b/testing/python/fixtures.py @@ -1138,7 +1138,6 @@ class TestFixtureUsages: values = reprec.getfailedcollections() assert len(values) == 1 - @pytest.mark.filterwarnings("ignore::pytest.PytestDeprecationWarning") def test_request_can_be_overridden(self, testdir): testdir.makepyfile( """ @@ -1151,7 +1150,7 @@ class TestFixtureUsages: assert request.a == 1 """ ) - reprec = testdir.inline_run() + reprec = testdir.inline_run("-Wignore::pytest.PytestDeprecationWarning") reprec.assertoutcome(passed=1) def test_usefixtures_marker(self, testdir): diff --git a/testing/test_warnings.py b/testing/test_warnings.py index 08a368521..2ce83ae88 100644 --- a/testing/test_warnings.py +++ b/testing/test_warnings.py @@ -528,6 +528,37 @@ def test_removed_in_pytest4_warning_as_error(testdir, change_default): result.stdout.fnmatch_lines(["* 1 passed in *"]) +@pytest.mark.parametrize("change_default", [None, "ini", "cmdline"]) +def test_deprecation_warning_as_error(testdir, change_default): + testdir.makepyfile( + """ + import warnings, pytest + def test(): + warnings.warn(pytest.PytestDeprecationWarning("some warning")) + """ + ) + if change_default == "ini": + testdir.makeini( + """ + [pytest] + filterwarnings = + ignore::pytest.PytestDeprecationWarning + """ + ) + + args = ( + ("-Wignore::pytest.PytestDeprecationWarning",) + if change_default == "cmdline" + else () + ) + result = testdir.runpytest(*args) + if change_default is None: + result.stdout.fnmatch_lines(["* 1 failed in *"]) + else: + assert change_default in ("ini", "cmdline") + result.stdout.fnmatch_lines(["* 1 passed in *"]) + + class TestAssertionWarnings: @staticmethod def assert_result_warns(result, msg):