From 4db3878a515a929a15dc7ca6087b97be82e77240 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Sat, 13 Jun 2020 09:33:10 -0300 Subject: [PATCH] Turn pytest deprecation warnings into errors Closes #5584 --- src/_pytest/warnings.py | 2 ++ testing/acceptance_test.py | 4 +-- testing/conftest.py | 4 +-- .../fixtures/custom_item/conftest.py | 9 ++++-- .../conftest.py | 4 +-- testing/python/collect.py | 6 ++-- testing/test_collection.py | 31 +++++++++++-------- testing/test_junitxml.py | 13 +++----- testing/test_skipping.py | 4 +-- testing/test_warnings.py | 3 -- 10 files changed, 43 insertions(+), 37 deletions(-) diff --git a/src/_pytest/warnings.py b/src/_pytest/warnings.py index 33b01b79707..3a8f2d8b33f 100644 --- a/src/_pytest/warnings.py +++ b/src/_pytest/warnings.py @@ -105,6 +105,8 @@ def catch_warnings_for_item( warnings.filterwarnings("always", category=DeprecationWarning) warnings.filterwarnings("always", category=PendingDeprecationWarning) + 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 for arg in inifilters: diff --git a/testing/acceptance_test.py b/testing/acceptance_test.py index e558c7f6781..2a386e2c6e4 100644 --- a/testing/acceptance_test.py +++ b/testing/acceptance_test.py @@ -302,10 +302,10 @@ def runtest(self): pass class MyCollector(pytest.File): def collect(self): - return [MyItem(name="xyz", parent=self)] + return [MyItem.from_parent(name="xyz", parent=self)] def pytest_collect_file(path, parent): if path.basename.startswith("conftest"): - return MyCollector(path, parent) + return MyCollector.from_parent(fspath=path, parent=parent) """ ) result = testdir.runpytest(c.basename + "::" + "xyz") diff --git a/testing/conftest.py b/testing/conftest.py index f430189489e..a667be42fcb 100644 --- a/testing/conftest.py +++ b/testing/conftest.py @@ -116,11 +116,11 @@ def dummy_yaml_custom_test(testdir): def pytest_collect_file(parent, path): if path.ext == ".yaml" and path.basename.startswith("test"): - return YamlFile(path, parent) + return YamlFile.from_parent(fspath=path, parent=parent) class YamlFile(pytest.File): def collect(self): - yield YamlItem(self.fspath.basename, self) + yield YamlItem.from_parent(name=self.fspath.basename, parent=self) class YamlItem(pytest.Item): def runtest(self): diff --git a/testing/example_scripts/fixtures/custom_item/conftest.py b/testing/example_scripts/fixtures/custom_item/conftest.py index 25299d72690..161934b58f7 100644 --- a/testing/example_scripts/fixtures/custom_item/conftest.py +++ b/testing/example_scripts/fixtures/custom_item/conftest.py @@ -1,10 +1,15 @@ import pytest -class CustomItem(pytest.Item, pytest.File): +class CustomItem(pytest.Item): def runtest(self): pass +class CustomFile(pytest.File): + def collect(self): + yield CustomItem.from_parent(name="foo", parent=self) + + def pytest_collect_file(path, parent): - return CustomItem(path, parent) + return CustomFile.from_parent(fspath=path, parent=parent) diff --git a/testing/example_scripts/issue88_initial_file_multinodes/conftest.py b/testing/example_scripts/issue88_initial_file_multinodes/conftest.py index aa5d878313c..a053a638a9f 100644 --- a/testing/example_scripts/issue88_initial_file_multinodes/conftest.py +++ b/testing/example_scripts/issue88_initial_file_multinodes/conftest.py @@ -3,11 +3,11 @@ class MyFile(pytest.File): def collect(self): - return [MyItem("hello", parent=self)] + return [MyItem.from_parent(name="hello", parent=self)] def pytest_collect_file(path, parent): - return MyFile(path, parent) + return MyFile.from_parent(fspath=path, parent=parent) class MyItem(pytest.Item): diff --git a/testing/python/collect.py b/testing/python/collect.py index e98a21f1cc5..d4a84ffea47 100644 --- a/testing/python/collect.py +++ b/testing/python/collect.py @@ -758,7 +758,7 @@ class MyModule(pytest.Module): pass def pytest_pycollect_makemodule(path, parent): if path.basename == "test_xyz.py": - return MyModule(path, parent) + return MyModule.from_parent(fspath=path, parent=parent) """ ) testdir.makepyfile("def test_some(): pass") @@ -832,7 +832,7 @@ class MyFunction(pytest.Function): pass def pytest_pycollect_makeitem(collector, name, obj): if name == "some": - return MyFunction(name, collector) + return MyFunction.from_parent(name=name, parent=collector) """ ) testdir.makepyfile("def some(): pass") @@ -869,7 +869,7 @@ def find_module(self, name, path=None): def pytest_collect_file(path, parent): if path.ext == ".narf": - return Module(path, parent)""" + return Module.from_parent(fspath=path, parent=parent)""" ) testdir.makefile( ".narf", diff --git a/testing/test_collection.py b/testing/test_collection.py index d7a9b0439aa..232417d0b70 100644 --- a/testing/test_collection.py +++ b/testing/test_collection.py @@ -282,7 +282,7 @@ def test_custom_repr_failure(self, testdir): """ import pytest def pytest_collect_file(path, parent): - return MyFile(path, parent) + return MyFile.from_parent(fspath=path, parent=parent) class MyError(Exception): pass class MyFile(pytest.File): @@ -401,7 +401,7 @@ class MyModule(pytest.Module): pass def pytest_collect_file(path, parent): if path.ext == ".py": - return MyModule(path, parent) + return MyModule.from_parent(fspath=path, parent=parent) """ ) testdir.mkdir("sub") @@ -419,7 +419,7 @@ class MyModule1(pytest.Module): pass def pytest_collect_file(path, parent): if path.ext == ".py": - return MyModule1(path, parent) + return MyModule1.from_parent(fspath=path, parent=parent) """ ) conf1.move(sub1.join(conf1.basename)) @@ -430,7 +430,7 @@ class MyModule2(pytest.Module): pass def pytest_collect_file(path, parent): if path.ext == ".py": - return MyModule2(path, parent) + return MyModule2.from_parent(fspath=path, parent=parent) """ ) conf2.move(sub2.join(conf2.basename)) @@ -537,10 +537,10 @@ def runtest(self): return # ok class SpecialFile(pytest.File): def collect(self): - return [SpecialItem(name="check", parent=self)] + return [SpecialItem.from_parent(name="check", parent=self)] def pytest_collect_file(path, parent): if path.basename == %r: - return SpecialFile(fspath=path, parent=parent) + return SpecialFile.from_parent(fspath=path, parent=parent) """ % p.basename ) @@ -761,18 +761,23 @@ def pytest_configure(config): class Plugin2(object): def pytest_collect_file(self, path, parent): if path.ext == ".abc": - return MyFile2(path, parent) + return MyFile2.from_parent(fspath=path, parent=parent) def pytest_collect_file(path, parent): if path.ext == ".abc": - return MyFile1(path, parent) + return MyFile1.from_parent(fspath=path, parent=parent) + + class MyFile1(pytest.File): + def collect(self): + yield Item1.from_parent(name="item1", parent=self) - class MyFile1(pytest.Item, pytest.File): - def runtest(self): - pass class MyFile2(pytest.File): def collect(self): - return [Item2("hello", parent=self)] + yield Item2.from_parent(name="item2", parent=self) + + class Item1(pytest.Item): + def runtest(self): + pass class Item2(pytest.Item): def runtest(self): @@ -783,7 +788,7 @@ def runtest(self): result = testdir.runpytest() assert result.ret == 0 result.stdout.fnmatch_lines(["*2 passed*"]) - res = testdir.runpytest("%s::hello" % p.basename) + res = testdir.runpytest("%s::item2" % p.basename) res.stdout.fnmatch_lines(["*1 passed*"]) diff --git a/testing/test_junitxml.py b/testing/test_junitxml.py index 5e5826b236a..9799adbbad3 100644 --- a/testing/test_junitxml.py +++ b/testing/test_junitxml.py @@ -906,11 +906,8 @@ def test_summing_simple(self, testdir, run_and_parse, xunit_family): import pytest def pytest_collect_file(path, parent): if path.ext == ".xyz": - return MyItem(path, parent) + return MyItem.from_parent(name=path.basename, parent=parent) class MyItem(pytest.Item): - def __init__(self, path, parent): - super(MyItem, self).__init__(path.basename, parent) - self.fspath = path def runtest(self): raise ValueError(42) def repr_failure(self, excinfo): @@ -1336,14 +1333,14 @@ def runtest(self): class FunCollector(pytest.File): def collect(self): return [ - FunItem('a', self), - NoFunItem('a', self), - NoFunItem('b', self), + FunItem.from_parent(name='a', parent=self), + NoFunItem.from_parent(name='a', parent=self), + NoFunItem.from_parent(name='b', parent=self), ] def pytest_collect_file(path, parent): if path.check(ext='.py'): - return FunCollector(path, parent) + return FunCollector.from_parent(fspath=path, parent=parent) """ ) diff --git a/testing/test_skipping.py b/testing/test_skipping.py index 8fceb37aa71..2ec7114f18e 100644 --- a/testing/test_skipping.py +++ b/testing/test_skipping.py @@ -1098,7 +1098,7 @@ def runtest(self): pytest.xfail("Expected Failure") def pytest_collect_file(path, parent): - return MyItem("foo", parent) + return MyItem.from_parent(name="foo", parent=parent) """ ) result = testdir.inline_run() @@ -1178,7 +1178,7 @@ def runtest(self): assert False def pytest_collect_file(path, parent): - return MyItem("foo", parent) + return MyItem.from_parent(name="foo", parent=parent) """ ) result = testdir.inline_run() diff --git a/testing/test_warnings.py b/testing/test_warnings.py index e21ccf42ae8..c3668180216 100644 --- a/testing/test_warnings.py +++ b/testing/test_warnings.py @@ -520,9 +520,6 @@ def test_hidden_by_system(self, testdir, monkeypatch): @pytest.mark.parametrize("change_default", [None, "ini", "cmdline"]) -@pytest.mark.skip( - reason="This test should be enabled again before pytest 6.0 is released" -) def test_deprecation_warning_as_error(testdir, change_default): """This ensures that PytestDeprecationWarnings raised by pytest are turned into errors.