Skip to content

Commit 4db3878

Browse files
committed
Turn pytest deprecation warnings into errors
Closes pytest-dev#5584
1 parent 0b58f73 commit 4db3878

File tree

10 files changed

+43
-37
lines changed

10 files changed

+43
-37
lines changed

src/_pytest/warnings.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ def catch_warnings_for_item(
105105
warnings.filterwarnings("always", category=DeprecationWarning)
106106
warnings.filterwarnings("always", category=PendingDeprecationWarning)
107107

108+
warnings.filterwarnings("error", category=pytest.PytestDeprecationWarning)
109+
108110
# filters should have this precedence: mark, cmdline options, ini
109111
# filters should be applied in the inverse order of precedence
110112
for arg in inifilters:

testing/acceptance_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,10 +302,10 @@ def runtest(self):
302302
pass
303303
class MyCollector(pytest.File):
304304
def collect(self):
305-
return [MyItem(name="xyz", parent=self)]
305+
return [MyItem.from_parent(name="xyz", parent=self)]
306306
def pytest_collect_file(path, parent):
307307
if path.basename.startswith("conftest"):
308-
return MyCollector(path, parent)
308+
return MyCollector.from_parent(fspath=path, parent=parent)
309309
"""
310310
)
311311
result = testdir.runpytest(c.basename + "::" + "xyz")

testing/conftest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,11 @@ def dummy_yaml_custom_test(testdir):
116116
117117
def pytest_collect_file(parent, path):
118118
if path.ext == ".yaml" and path.basename.startswith("test"):
119-
return YamlFile(path, parent)
119+
return YamlFile.from_parent(fspath=path, parent=parent)
120120
121121
class YamlFile(pytest.File):
122122
def collect(self):
123-
yield YamlItem(self.fspath.basename, self)
123+
yield YamlItem.from_parent(name=self.fspath.basename, parent=self)
124124
125125
class YamlItem(pytest.Item):
126126
def runtest(self):
Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
import pytest
22

33

4-
class CustomItem(pytest.Item, pytest.File):
4+
class CustomItem(pytest.Item):
55
def runtest(self):
66
pass
77

88

9+
class CustomFile(pytest.File):
10+
def collect(self):
11+
yield CustomItem.from_parent(name="foo", parent=self)
12+
13+
914
def pytest_collect_file(path, parent):
10-
return CustomItem(path, parent)
15+
return CustomFile.from_parent(fspath=path, parent=parent)

testing/example_scripts/issue88_initial_file_multinodes/conftest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33

44
class MyFile(pytest.File):
55
def collect(self):
6-
return [MyItem("hello", parent=self)]
6+
return [MyItem.from_parent(name="hello", parent=self)]
77

88

99
def pytest_collect_file(path, parent):
10-
return MyFile(path, parent)
10+
return MyFile.from_parent(fspath=path, parent=parent)
1111

1212

1313
class MyItem(pytest.Item):

testing/python/collect.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -758,7 +758,7 @@ class MyModule(pytest.Module):
758758
pass
759759
def pytest_pycollect_makemodule(path, parent):
760760
if path.basename == "test_xyz.py":
761-
return MyModule(path, parent)
761+
return MyModule.from_parent(fspath=path, parent=parent)
762762
"""
763763
)
764764
testdir.makepyfile("def test_some(): pass")
@@ -832,7 +832,7 @@ class MyFunction(pytest.Function):
832832
pass
833833
def pytest_pycollect_makeitem(collector, name, obj):
834834
if name == "some":
835-
return MyFunction(name, collector)
835+
return MyFunction.from_parent(name=name, parent=collector)
836836
"""
837837
)
838838
testdir.makepyfile("def some(): pass")
@@ -869,7 +869,7 @@ def find_module(self, name, path=None):
869869
870870
def pytest_collect_file(path, parent):
871871
if path.ext == ".narf":
872-
return Module(path, parent)"""
872+
return Module.from_parent(fspath=path, parent=parent)"""
873873
)
874874
testdir.makefile(
875875
".narf",

testing/test_collection.py

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ def test_custom_repr_failure(self, testdir):
282282
"""
283283
import pytest
284284
def pytest_collect_file(path, parent):
285-
return MyFile(path, parent)
285+
return MyFile.from_parent(fspath=path, parent=parent)
286286
class MyError(Exception):
287287
pass
288288
class MyFile(pytest.File):
@@ -401,7 +401,7 @@ class MyModule(pytest.Module):
401401
pass
402402
def pytest_collect_file(path, parent):
403403
if path.ext == ".py":
404-
return MyModule(path, parent)
404+
return MyModule.from_parent(fspath=path, parent=parent)
405405
"""
406406
)
407407
testdir.mkdir("sub")
@@ -419,7 +419,7 @@ class MyModule1(pytest.Module):
419419
pass
420420
def pytest_collect_file(path, parent):
421421
if path.ext == ".py":
422-
return MyModule1(path, parent)
422+
return MyModule1.from_parent(fspath=path, parent=parent)
423423
"""
424424
)
425425
conf1.move(sub1.join(conf1.basename))
@@ -430,7 +430,7 @@ class MyModule2(pytest.Module):
430430
pass
431431
def pytest_collect_file(path, parent):
432432
if path.ext == ".py":
433-
return MyModule2(path, parent)
433+
return MyModule2.from_parent(fspath=path, parent=parent)
434434
"""
435435
)
436436
conf2.move(sub2.join(conf2.basename))
@@ -537,10 +537,10 @@ def runtest(self):
537537
return # ok
538538
class SpecialFile(pytest.File):
539539
def collect(self):
540-
return [SpecialItem(name="check", parent=self)]
540+
return [SpecialItem.from_parent(name="check", parent=self)]
541541
def pytest_collect_file(path, parent):
542542
if path.basename == %r:
543-
return SpecialFile(fspath=path, parent=parent)
543+
return SpecialFile.from_parent(fspath=path, parent=parent)
544544
"""
545545
% p.basename
546546
)
@@ -761,18 +761,23 @@ def pytest_configure(config):
761761
class Plugin2(object):
762762
def pytest_collect_file(self, path, parent):
763763
if path.ext == ".abc":
764-
return MyFile2(path, parent)
764+
return MyFile2.from_parent(fspath=path, parent=parent)
765765
766766
def pytest_collect_file(path, parent):
767767
if path.ext == ".abc":
768-
return MyFile1(path, parent)
768+
return MyFile1.from_parent(fspath=path, parent=parent)
769+
770+
class MyFile1(pytest.File):
771+
def collect(self):
772+
yield Item1.from_parent(name="item1", parent=self)
769773
770-
class MyFile1(pytest.Item, pytest.File):
771-
def runtest(self):
772-
pass
773774
class MyFile2(pytest.File):
774775
def collect(self):
775-
return [Item2("hello", parent=self)]
776+
yield Item2.from_parent(name="item2", parent=self)
777+
778+
class Item1(pytest.Item):
779+
def runtest(self):
780+
pass
776781
777782
class Item2(pytest.Item):
778783
def runtest(self):
@@ -783,7 +788,7 @@ def runtest(self):
783788
result = testdir.runpytest()
784789
assert result.ret == 0
785790
result.stdout.fnmatch_lines(["*2 passed*"])
786-
res = testdir.runpytest("%s::hello" % p.basename)
791+
res = testdir.runpytest("%s::item2" % p.basename)
787792
res.stdout.fnmatch_lines(["*1 passed*"])
788793

789794

testing/test_junitxml.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -906,11 +906,8 @@ def test_summing_simple(self, testdir, run_and_parse, xunit_family):
906906
import pytest
907907
def pytest_collect_file(path, parent):
908908
if path.ext == ".xyz":
909-
return MyItem(path, parent)
909+
return MyItem.from_parent(name=path.basename, parent=parent)
910910
class MyItem(pytest.Item):
911-
def __init__(self, path, parent):
912-
super(MyItem, self).__init__(path.basename, parent)
913-
self.fspath = path
914911
def runtest(self):
915912
raise ValueError(42)
916913
def repr_failure(self, excinfo):
@@ -1336,14 +1333,14 @@ def runtest(self):
13361333
class FunCollector(pytest.File):
13371334
def collect(self):
13381335
return [
1339-
FunItem('a', self),
1340-
NoFunItem('a', self),
1341-
NoFunItem('b', self),
1336+
FunItem.from_parent(name='a', parent=self),
1337+
NoFunItem.from_parent(name='a', parent=self),
1338+
NoFunItem.from_parent(name='b', parent=self),
13421339
]
13431340
13441341
def pytest_collect_file(path, parent):
13451342
if path.check(ext='.py'):
1346-
return FunCollector(path, parent)
1343+
return FunCollector.from_parent(fspath=path, parent=parent)
13471344
"""
13481345
)
13491346

testing/test_skipping.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1098,7 +1098,7 @@ def runtest(self):
10981098
pytest.xfail("Expected Failure")
10991099
11001100
def pytest_collect_file(path, parent):
1101-
return MyItem("foo", parent)
1101+
return MyItem.from_parent(name="foo", parent=parent)
11021102
"""
11031103
)
11041104
result = testdir.inline_run()
@@ -1178,7 +1178,7 @@ def runtest(self):
11781178
assert False
11791179
11801180
def pytest_collect_file(path, parent):
1181-
return MyItem("foo", parent)
1181+
return MyItem.from_parent(name="foo", parent=parent)
11821182
"""
11831183
)
11841184
result = testdir.inline_run()

testing/test_warnings.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -520,9 +520,6 @@ def test_hidden_by_system(self, testdir, monkeypatch):
520520

521521

522522
@pytest.mark.parametrize("change_default", [None, "ini", "cmdline"])
523-
@pytest.mark.skip(
524-
reason="This test should be enabled again before pytest 6.0 is released"
525-
)
526523
def test_deprecation_warning_as_error(testdir, change_default):
527524
"""This ensures that PytestDeprecationWarnings raised by pytest are turned into errors.
528525

0 commit comments

Comments
 (0)