Skip to content

Commit

Permalink
Turn pytest deprecation warnings into errors
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoddemus committed Jul 11, 2020
1 parent 0b58f73 commit 4db3878
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 37 deletions.
2 changes: 2 additions & 0 deletions src/_pytest/warnings.py
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions testing/acceptance_test.py
Expand Up @@ -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")
Expand Down
4 changes: 2 additions & 2 deletions testing/conftest.py
Expand Up @@ -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):
Expand Down
9 changes: 7 additions & 2 deletions 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)
Expand Up @@ -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):
Expand Down
6 changes: 3 additions & 3 deletions testing/python/collect.py
Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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",
Expand Down
31 changes: 18 additions & 13 deletions testing/test_collection.py
Expand Up @@ -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):
Expand Down Expand Up @@ -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")
Expand All @@ -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))
Expand All @@ -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))
Expand Down Expand Up @@ -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
)
Expand Down Expand Up @@ -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):
Expand All @@ -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*"])


Expand Down
13 changes: 5 additions & 8 deletions testing/test_junitxml.py
Expand Up @@ -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):
Expand Down Expand Up @@ -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)
"""
)

Expand Down
4 changes: 2 additions & 2 deletions testing/test_skipping.py
Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
3 changes: 0 additions & 3 deletions testing/test_warnings.py
Expand Up @@ -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.
Expand Down

0 comments on commit 4db3878

Please sign in to comment.