Skip to content

Commit 748ac78

Browse files
committed
Change RemovedInPytest4Warnings to errors by default
To keep existing tests which emit RemovedInPytest4Warnings running, decided to go with a command line option because: * Is harder to integrate an ini option with tests which already use an ini file * It also marks tests which need to be removed/updated in 4.1, when RemovedInPytest4Warning and related functionality are removed. Fix pytest-dev#3737
1 parent 5dd509c commit 748ac78

16 files changed

+132
-59
lines changed

changelog/3737.deprecation.rst

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
**RemovedInPytest4Warnings are now errors by default.**
2+
3+
Following our plan to remove deprecated features with as little disruption as
4+
possible, all warnings of type ``RemovedInPytest4Warnings`` now generate errors
5+
instead of warning messages.
6+
7+
**The affected features will be effectively removed in pytest 4.1**, so please consult the
8+
`Deprecations and Removals <https://docs.pytest.org/en/latest/deprecations.html>`__
9+
section in the docs for directions on how to update existing code.
10+
11+
In the pytest ``4.0.X`` series, it is possible to change the errors back into warnings as a stop
12+
gap measure by adding this to your ``pytest.ini`` file:
13+
14+
.. code-block:: ini
15+
16+
[pytest]
17+
filterwarnings =
18+
ignore::pytest.RemovedInPytest4Warning
19+
20+
But this will stop working when pytest ``4.1`` is released.
21+
22+
**If you have concerns** about the removal of a specific feature, please add a
23+
comment to `#4348 <https://github.com/pytest-dev/pytest/issues/4348>`__.

doc/en/example/assertion/failure_demo.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import six
22

33
import _pytest._code
4+
import pytest
45
from pytest import raises
56

67

@@ -16,15 +17,11 @@ def otherfunc_multi(a, b):
1617
assert a == b
1718

1819

20+
@pytest.mark.parametrize("param1, param2", [(3, 6)])
1921
def test_generative(param1, param2):
2022
assert param1 * 2 < param2
2123

2224

23-
def pytest_generate_tests(metafunc):
24-
if "param1" in metafunc.fixturenames:
25-
metafunc.addcall(funcargs=dict(param1=3, param2=6))
26-
27-
2825
class TestFailing(object):
2926
def test_simple(self):
3027
def f():

src/_pytest/warnings.py

+4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import pytest
1010
from _pytest import compat
1111

12+
SHOW_PYTEST_WARNINGS_ARG = "-Walways::pytest.RemovedInPytest4Warning"
13+
1214

1315
def _setoption(wmod, arg):
1416
"""
@@ -77,6 +79,8 @@ def catch_warnings_for_item(config, ihook, when, item):
7779
warnings.filterwarnings("always", category=DeprecationWarning)
7880
warnings.filterwarnings("always", category=PendingDeprecationWarning)
7981

82+
warnings.filterwarnings("error", category=pytest.RemovedInPytest4Warning)
83+
8084
# filters should have this precedence: mark, cmdline options, ini
8185
# filters should be applied in the inverse order of precedence
8286
for arg in inifilters:

testing/acceptance_test.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import pytest
1515
from _pytest.main import EXIT_NOTESTSCOLLECTED
1616
from _pytest.main import EXIT_USAGEERROR
17+
from _pytest.warnings import SHOW_PYTEST_WARNINGS_ARG
1718

1819

1920
def prepend_pythonpath(*dirs):
@@ -306,7 +307,7 @@ def pytest_runtest_setup(item):
306307
"""
307308
)
308309
p = testdir.makepyfile("""def test_func(x): pass""")
309-
res = testdir.runpytest(p)
310+
res = testdir.runpytest(p, SHOW_PYTEST_WARNINGS_ARG)
310311
assert res.ret == 0
311312
res.stdout.fnmatch_lines(["*1 skipped*"])
312313

@@ -320,7 +321,9 @@ def test_func(i):
320321
pass
321322
"""
322323
)
323-
res = testdir.runpytest(p.basename + "::" + "test_func[1]")
324+
res = testdir.runpytest(
325+
p.basename + "::" + "test_func[1]", SHOW_PYTEST_WARNINGS_ARG
326+
)
324327
assert res.ret == 0
325328
res.stdout.fnmatch_lines(["*1 passed*"])
326329

testing/deprecated_test.py

+9-10
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
import os
66

77
import pytest
8+
from _pytest.warnings import SHOW_PYTEST_WARNINGS_ARG
89

910
pytestmark = pytest.mark.pytester_example_path("deprecated")
1011

1112

12-
@pytest.mark.filterwarnings("default")
1313
def test_yield_tests_deprecation(testdir):
1414
testdir.makepyfile(
1515
"""
@@ -23,7 +23,7 @@ def test_gen2():
2323
yield func1, 1, 1
2424
"""
2525
)
26-
result = testdir.runpytest()
26+
result = testdir.runpytest(SHOW_PYTEST_WARNINGS_ARG)
2727
result.stdout.fnmatch_lines(
2828
[
2929
"*test_yield_tests_deprecation.py:3:*yield tests are deprecated*",
@@ -41,7 +41,7 @@ def test_foo(request):
4141
print(request.node.Module)
4242
"""
4343
)
44-
result = testdir.runpytest()
44+
result = testdir.runpytest(SHOW_PYTEST_WARNINGS_ARG)
4545
result.stdout.fnmatch_lines(
4646
[
4747
"*test_compat_properties_deprecation.py:2:*usage of Function.Module is deprecated, "
@@ -63,7 +63,7 @@ def test_foo(fix):
6363
assert fix == 1
6464
"""
6565
)
66-
result = testdir.runpytest()
66+
result = testdir.runpytest(SHOW_PYTEST_WARNINGS_ARG)
6767
result.stdout.fnmatch_lines(
6868
[
6969
"*test_cached_setup_deprecation.py:4:*cached_setup is deprecated*",
@@ -93,7 +93,7 @@ def test_foo(self):
9393
pass
9494
"""
9595
)
96-
result = testdir.runpytest()
96+
result = testdir.runpytest(SHOW_PYTEST_WARNINGS_ARG)
9797
result.stdout.fnmatch_lines(
9898
[
9999
'*test_custom_class_deprecation.py:1:*"Class" objects in collectors of type "MyModule*',
@@ -102,7 +102,6 @@ def test_foo(self):
102102
)
103103

104104

105-
@pytest.mark.filterwarnings("default")
106105
def test_funcarg_prefix_deprecation(testdir):
107106
testdir.makepyfile(
108107
"""
@@ -113,7 +112,7 @@ def test_funcarg_prefix(value):
113112
assert value == 10
114113
"""
115114
)
116-
result = testdir.runpytest("-ra")
115+
result = testdir.runpytest("-ra", SHOW_PYTEST_WARNINGS_ARG)
117116
result.stdout.fnmatch_lines(
118117
[
119118
(
@@ -198,7 +197,6 @@ def test():
198197
)
199198

200199

201-
@pytest.mark.filterwarnings("always:Metafunc.addcall is deprecated")
202200
def test_metafunc_addcall_deprecated(testdir):
203201
testdir.makepyfile(
204202
"""
@@ -209,7 +207,7 @@ def test_func(i):
209207
pass
210208
"""
211209
)
212-
res = testdir.runpytest("-s")
210+
res = testdir.runpytest("-s", SHOW_PYTEST_WARNINGS_ARG)
213211
assert res.ret == 0
214212
res.stdout.fnmatch_lines(
215213
["*Metafunc.addcall is deprecated*", "*2 passed, 2 warnings*"]
@@ -263,7 +261,7 @@ def test_func():
263261
pass
264262
"""
265263
)
266-
res = testdir.runpytest()
264+
res = testdir.runpytest(SHOW_PYTEST_WARNINGS_ARG)
267265
assert res.ret == 0
268266
msg = str(PYTEST_PLUGINS_FROM_NON_TOP_LEVEL_CONFTEST).splitlines()[0]
269267
res.stdout.fnmatch_lines(
@@ -292,6 +290,7 @@ def test_pytest_plugins_in_non_top_level_conftest_deprecated_pyargs(
292290
testdir.syspathinsert(testdir.tmpdir.join("src"))
293291

294292
args = ("--pyargs", "pkg") if use_pyargs else ()
293+
args += (SHOW_PYTEST_WARNINGS_ARG,)
295294
res = testdir.runpytest(*args)
296295
assert res.ret == 0
297296
msg = str(PYTEST_PLUGINS_FROM_NON_TOP_LEVEL_CONFTEST).splitlines()[0]

testing/python/collect.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import pytest
88
from _pytest.main import EXIT_NOTESTSCOLLECTED
99
from _pytest.nodes import Collector
10+
from _pytest.warnings import SHOW_PYTEST_WARNINGS_ARG
1011

1112

1213
class TestModule(object):
@@ -370,7 +371,7 @@ def assert_order_of_execution():
370371
yield assert_order_of_execution
371372
"""
372373
)
373-
reprec = testdir.inline_run(o)
374+
reprec = testdir.inline_run(o, SHOW_PYTEST_WARNINGS_ARG)
374375
passed, skipped, failed = reprec.countoutcomes()
375376
assert passed == 7
376377
assert not skipped and not failed
@@ -404,7 +405,7 @@ def assert_order_of_execution():
404405
yield assert_order_of_execution
405406
"""
406407
)
407-
reprec = testdir.inline_run(o)
408+
reprec = testdir.inline_run(o, SHOW_PYTEST_WARNINGS_ARG)
408409
passed, skipped, failed = reprec.countoutcomes()
409410
assert passed == 4
410411
assert not skipped and not failed
@@ -448,7 +449,7 @@ def test_setuplist():
448449
assert setuplist[1] != setuplist[2], setuplist
449450
"""
450451
)
451-
reprec = testdir.inline_run(o, "-v")
452+
reprec = testdir.inline_run(o, "-v", SHOW_PYTEST_WARNINGS_ARG)
452453
passed, skipped, failed = reprec.countoutcomes()
453454
assert passed == 4
454455
assert not skipped and not failed
@@ -1380,7 +1381,7 @@ def test_hello():
13801381
pass
13811382
"""
13821383
)
1383-
result = testdir.runpytest()
1384+
result = testdir.runpytest(SHOW_PYTEST_WARNINGS_ARG)
13841385
result.stdout.fnmatch_lines(["*1 passed*"])
13851386

13861387

@@ -1407,7 +1408,7 @@ def test_hello(self):
14071408
pass
14081409
"""
14091410
)
1410-
result = testdir.runpytest("--collect-only")
1411+
result = testdir.runpytest("--collect-only", SHOW_PYTEST_WARNINGS_ARG)
14111412
result.stdout.fnmatch_lines(
14121413
["*MyClass*", "*MyInstance*", "*MyFunction*test_hello*"]
14131414
)

testing/python/fixture.py

+11-8
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from _pytest.fixtures import FixtureRequest
99
from _pytest.pathlib import Path
1010
from _pytest.pytester import get_public_names
11+
from _pytest.warnings import SHOW_PYTEST_WARNINGS_ARG
1112

1213

1314
def test_getfuncargnames():
@@ -975,7 +976,8 @@ def test_func1(something):
975976
class TestClass(object):
976977
def test_func1a(self, something):
977978
assert something == "hello"
978-
"""
979+
""",
980+
SHOW_PYTEST_WARNINGS_ARG,
979981
)
980982
reprec.assertoutcome(passed=2)
981983

@@ -997,7 +999,8 @@ def test_func1a(self, something):
997999
assert something == "hello"
9981000
def test_func2b(self, something):
9991001
assert something == "hello"
1000-
"""
1002+
""",
1003+
SHOW_PYTEST_WARNINGS_ARG,
10011004
)
10021005
reprec.assertoutcome(passed=4)
10031006

@@ -1057,7 +1060,7 @@ def test_two_different_setups(arg1, arg2):
10571060
assert arg1 != arg2
10581061
"""
10591062
)
1060-
result = testdir.runpytest("-v")
1063+
result = testdir.runpytest("-v", SHOW_PYTEST_WARNINGS_ARG)
10611064
result.stdout.fnmatch_lines(["*1 passed*"])
10621065

10631066
def test_request_cached_setup_getfixturevalue(self, testdir):
@@ -1076,7 +1079,7 @@ def test_two_funcarg(arg1):
10761079
assert arg1 == 11
10771080
"""
10781081
)
1079-
result = testdir.runpytest("-v")
1082+
result = testdir.runpytest("-v", SHOW_PYTEST_WARNINGS_ARG)
10801083
result.stdout.fnmatch_lines(["*1 passed*"])
10811084

10821085
def test_request_cached_setup_functional(self, testdir):
@@ -1107,7 +1110,7 @@ def test_check_test0_has_teardown_correct():
11071110
assert test_0.values == [2]
11081111
"""
11091112
)
1110-
result = testdir.runpytest("-v")
1113+
result = testdir.runpytest("-v", SHOW_PYTEST_WARNINGS_ARG)
11111114
result.stdout.fnmatch_lines(["*3 passed*"])
11121115

11131116
def test_issue117_sessionscopeteardown(self, testdir):
@@ -1126,7 +1129,7 @@ def test_func(app):
11261129
pass
11271130
"""
11281131
)
1129-
result = testdir.runpytest()
1132+
result = testdir.runpytest(SHOW_PYTEST_WARNINGS_ARG)
11301133
assert result.ret != 0
11311134
result.stdout.fnmatch_lines(["*3/x*", "*ZeroDivisionError*"])
11321135

@@ -1868,7 +1871,7 @@ def f(hello):
18681871
yield f, -3
18691872
"""
18701873
)
1871-
reprec = testdir.inline_run()
1874+
reprec = testdir.inline_run(SHOW_PYTEST_WARNINGS_ARG)
18721875
reprec.assertoutcome(passed=2)
18731876

18741877
def test_funcarg_and_setup(self, testdir):
@@ -2348,7 +2351,7 @@ def test_1(arg):
23482351
"""
23492352
% method
23502353
)
2351-
result = testdir.runpytest()
2354+
result = testdir.runpytest(SHOW_PYTEST_WARNINGS_ARG)
23522355
assert result.ret != 0
23532356
result.stdout.fnmatch_lines(
23542357
["*ScopeMismatch*You tried*function*session*request*"]

0 commit comments

Comments
 (0)