Skip to content

Commit 510a608

Browse files
authored
Merge pull request #1758 from nicoddemus/deprecated-module
Add deprecation module to centralize deprecation messages and bits of…
2 parents c40dcb3 + 6c8b0a2 commit 510a608

File tree

8 files changed

+85
-63
lines changed

8 files changed

+85
-63
lines changed

_pytest/config.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,8 @@ def _prepareconfig(args=None, plugins=None):
115115
if not isinstance(args, str):
116116
raise ValueError("not a string or argument list: %r" % (args,))
117117
args = shlex.split(args, posix=sys.platform != "win32")
118-
# we want to remove this way of passing arguments to pytest.main()
119-
# in pytest-4.0
120-
warning = ('passing a string to pytest.main() is deprecated, '
121-
'pass a list of arguments instead.')
118+
from _pytest import deprecated
119+
warning = deprecated.MAIN_STR_ARGS
122120
config = get_config()
123121
pluginmanager = config.pluginmanager
124122
try:

_pytest/deprecated.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
"""
2+
This module contains deprecation messages and bits of code used elsewhere in the codebase
3+
that is planned to be removed in the next pytest release.
4+
5+
Keeping it in a central location makes it easy to track what is deprecated and should
6+
be removed when the time comes.
7+
"""
8+
9+
10+
MAIN_STR_ARGS = 'passing a string to pytest.main() is deprecated, ' \
11+
'pass a list of arguments instead.'
12+
13+
YIELD_TESTS = 'yield tests are deprecated, and scheduled to be removed in pytest 4.0'
14+
15+
FUNCARG_PREFIX = (
16+
'{name}: declaring fixtures using "pytest_funcarg__" prefix is deprecated '
17+
'and scheduled to be removed in pytest 4.0. '
18+
'Please remove the prefix and use the @pytest.fixture decorator instead.')
19+
20+
GETFUNCARGVALUE = "use of getfuncargvalue is deprecated, use getfixturevalue"
21+

_pytest/fixtures.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -450,8 +450,9 @@ def getfixturevalue(self, argname):
450450

451451
def getfuncargvalue(self, argname):
452452
""" Deprecated, use getfixturevalue. """
453+
from _pytest import deprecated
453454
warnings.warn(
454-
"use of getfuncargvalue is deprecated, use getfixturevalue",
455+
deprecated.GETFUNCARGVALUE,
455456
DeprecationWarning)
456457
return self.getfixturevalue(argname)
457458

@@ -885,10 +886,6 @@ def yield_fixture(scope="function", params=None, autouse=False, ids=None, name=N
885886

886887

887888
defaultfuncargprefixmarker = fixture()
888-
funcarg_prefix_warning = (
889-
'{name}: declaring fixtures using "pytest_funcarg__" prefix is deprecated '
890-
'and scheduled to be removed in pytest 4.0. '
891-
'Please remove the prefix and use the @pytest.fixture decorator instead.')
892889

893890

894891
@fixture(scope="session")
@@ -1071,7 +1068,8 @@ def parsefactories(self, node_or_obj, nodeid=NOTSET, unittest=False):
10711068
if not callable(obj):
10721069
continue
10731070
marker = defaultfuncargprefixmarker
1074-
self.config.warn('C1', funcarg_prefix_warning.format(name=name))
1071+
from _pytest import deprecated
1072+
self.config.warn('C1', deprecated.FUNCARG_PREFIX.format(name=name))
10751073
name = name[len(self._argprefix):]
10761074
elif not isinstance(marker, FixtureFunctionMarker):
10771075
# magic globals with __getattr__ might have got us a wrong

_pytest/python.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,7 @@ def collect(self):
588588
# test generators are seen as collectors but they also
589589
# invoke setup/teardown on popular request
590590
# (induced by the common "test_*" naming shared with normal tests)
591+
from _pytest import deprecated
591592
self.session._setupstate.prepare(self)
592593
# see FunctionMixin.setup and test_setupstate_is_preserved_134
593594
self._preservedparent = self.parent.obj
@@ -605,8 +606,7 @@ def collect(self):
605606
raise ValueError("%r generated tests with non-unique name %r" %(self, name))
606607
seen[name] = True
607608
l.append(self.Function(name, self, args=args, callobj=call))
608-
msg = 'yield tests are deprecated, and scheduled to be removed in pytest 4.0'
609-
self.config.warn('C1', msg, fslocation=self.fspath)
609+
self.config.warn('C1', deprecated.YIELD_TESTS, fslocation=self.fspath)
610610
return l
611611

612612
def getcallargs(self, obj):

testing/acceptance_test.py

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -763,51 +763,3 @@ def test_setup_function(self, testdir):
763763
* call *test_1*
764764
""")
765765

766-
767-
def test_yield_tests_deprecation(testdir):
768-
testdir.makepyfile("""
769-
def func1(arg, arg2):
770-
assert arg == arg2
771-
def test_gen():
772-
yield "m1", func1, 15, 3*5
773-
yield "m2", func1, 42, 6*7
774-
""")
775-
result = testdir.runpytest('-ra')
776-
result.stdout.fnmatch_lines([
777-
'*yield tests are deprecated, and scheduled to be removed in pytest 4.0*',
778-
'*2 passed*',
779-
])
780-
781-
782-
def test_funcarg_prefix_deprecation(testdir):
783-
testdir.makepyfile("""
784-
def pytest_funcarg__value():
785-
return 10
786-
787-
def test_funcarg_prefix(value):
788-
assert value == 10
789-
""")
790-
result = testdir.runpytest('-ra')
791-
result.stdout.fnmatch_lines([
792-
('WC1 None pytest_funcarg__value: '
793-
'declaring fixtures using "pytest_funcarg__" prefix is deprecated '
794-
'and scheduled to be removed in pytest 4.0. '
795-
'Please remove the prefix and use the @pytest.fixture decorator instead.'),
796-
'*1 passed*',
797-
])
798-
799-
800-
def test_str_args_deprecated(tmpdir, testdir):
801-
"""Deprecate passing strings to pytest.main(). Scheduled for removal in pytest-4.0."""
802-
warnings = []
803-
804-
class Collect:
805-
def pytest_logwarning(self, message):
806-
warnings.append(message)
807-
808-
ret = pytest.main("%s -x" % tmpdir, plugins=[Collect()])
809-
testdir.delete_loaded_modules()
810-
msg = ('passing a string to pytest.main() is deprecated, '
811-
'pass a list of arguments instead.')
812-
assert msg in warnings
813-
assert ret == EXIT_NOTESTSCOLLECTED

testing/deprecated_test.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import pytest
2+
3+
4+
def test_yield_tests_deprecation(testdir):
5+
testdir.makepyfile("""
6+
def func1(arg, arg2):
7+
assert arg == arg2
8+
def test_gen():
9+
yield "m1", func1, 15, 3*5
10+
yield "m2", func1, 42, 6*7
11+
""")
12+
result = testdir.runpytest('-ra')
13+
result.stdout.fnmatch_lines([
14+
'*yield tests are deprecated, and scheduled to be removed in pytest 4.0*',
15+
'*2 passed*',
16+
])
17+
18+
19+
def test_funcarg_prefix_deprecation(testdir):
20+
testdir.makepyfile("""
21+
def pytest_funcarg__value():
22+
return 10
23+
24+
def test_funcarg_prefix(value):
25+
assert value == 10
26+
""")
27+
result = testdir.runpytest('-ra')
28+
result.stdout.fnmatch_lines([
29+
('WC1 None pytest_funcarg__value: '
30+
'declaring fixtures using "pytest_funcarg__" prefix is deprecated '
31+
'and scheduled to be removed in pytest 4.0. '
32+
'Please remove the prefix and use the @pytest.fixture decorator instead.'),
33+
'*1 passed*',
34+
])
35+
36+
37+
def test_str_args_deprecated(tmpdir, testdir):
38+
"""Deprecate passing strings to pytest.main(). Scheduled for removal in pytest-4.0."""
39+
from _pytest.main import EXIT_NOTESTSCOLLECTED
40+
warnings = []
41+
42+
class Collect:
43+
def pytest_logwarning(self, message):
44+
warnings.append(message)
45+
46+
ret = pytest.main("%s -x" % tmpdir, plugins=[Collect()])
47+
testdir.delete_loaded_modules()
48+
msg = ('passing a string to pytest.main() is deprecated, '
49+
'pass a list of arguments instead.')
50+
assert msg in warnings
51+
assert ret == EXIT_NOTESTSCOLLECTED
52+
53+
54+
def test_getfuncargvalue_is_deprecated(request):
55+
pytest.deprecated_call(request.getfuncargvalue, 'tmpdir')

testing/python/fixture.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2941,5 +2941,4 @@ def test_foo(request):
29412941
""".format(fixfile.strpath, testfile.basename))
29422942

29432943

2944-
def test_getfuncargvalue_is_deprecated(request):
2945-
pytest.deprecated_call(request.getfuncargvalue, 'tmpdir')
2944+

testing/test_tmpdir.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ def test_func(tmpdir): pass
2929
assert bn == "qwe__abc"
3030

3131
def test_ensuretemp(recwarn):
32-
#pytest.deprecated_call(pytest.ensuretemp, 'hello')
3332
d1 = pytest.ensuretemp('hello')
3433
d2 = pytest.ensuretemp('hello')
3534
assert d1 == d2

0 commit comments

Comments
 (0)