Add deprecation module to centralize deprecation messages and bits of code

This commit is contained in:
Bruno Oliveira 2016-07-23 11:56:04 -03:00
parent ae0798522f
commit 6c8b0a28e1
8 changed files with 85 additions and 63 deletions

View File

@ -115,10 +115,8 @@ def _prepareconfig(args=None, plugins=None):
if not isinstance(args, str):
raise ValueError("not a string or argument list: %r" % (args,))
args = shlex.split(args, posix=sys.platform != "win32")
# we want to remove this way of passing arguments to pytest.main()
# in pytest-4.0
warning = ('passing a string to pytest.main() is deprecated, '
'pass a list of arguments instead.')
from _pytest import deprecated
warning = deprecated.MAIN_STR_ARGS
config = get_config()
pluginmanager = config.pluginmanager
try:

21
_pytest/deprecated.py Normal file
View File

@ -0,0 +1,21 @@
"""
This module contains deprecation messages and bits of code used elsewhere in the codebase
that is planned to be removed in the next pytest release.
Keeping it in a central location makes it easy to track what is deprecated and should
be removed when the time comes.
"""
MAIN_STR_ARGS = 'passing a string to pytest.main() is deprecated, ' \
'pass a list of arguments instead.'
YIELD_TESTS = 'yield tests are deprecated, and scheduled to be removed in pytest 4.0'
FUNCARG_PREFIX = (
'{name}: declaring fixtures using "pytest_funcarg__" prefix is deprecated '
'and scheduled to be removed in pytest 4.0. '
'Please remove the prefix and use the @pytest.fixture decorator instead.')
GETFUNCARGVALUE = "use of getfuncargvalue is deprecated, use getfixturevalue"

View File

@ -450,8 +450,9 @@ class FixtureRequest(FuncargnamesCompatAttr):
def getfuncargvalue(self, argname):
""" Deprecated, use getfixturevalue. """
from _pytest import deprecated
warnings.warn(
"use of getfuncargvalue is deprecated, use getfixturevalue",
deprecated.GETFUNCARGVALUE,
DeprecationWarning)
return self.getfixturevalue(argname)
@ -880,10 +881,6 @@ def yield_fixture(scope="function", params=None, autouse=False, ids=None, name=N
defaultfuncargprefixmarker = fixture()
funcarg_prefix_warning = (
'{name}: declaring fixtures using "pytest_funcarg__" prefix is deprecated '
'and scheduled to be removed in pytest 4.0. '
'Please remove the prefix and use the @pytest.fixture decorator instead.')
@fixture(scope="session")
@ -1066,7 +1063,8 @@ class FixtureManager:
if not callable(obj):
continue
marker = defaultfuncargprefixmarker
self.config.warn('C1', funcarg_prefix_warning.format(name=name))
from _pytest import deprecated
self.config.warn('C1', deprecated.FUNCARG_PREFIX.format(name=name))
name = name[len(self._argprefix):]
elif not isinstance(marker, FixtureFunctionMarker):
# magic globals with __getattr__ might have got us a wrong

View File

@ -585,6 +585,7 @@ class Generator(FunctionMixin, PyCollector):
# test generators are seen as collectors but they also
# invoke setup/teardown on popular request
# (induced by the common "test_*" naming shared with normal tests)
from _pytest import deprecated
self.session._setupstate.prepare(self)
# see FunctionMixin.setup and test_setupstate_is_preserved_134
self._preservedparent = self.parent.obj
@ -602,8 +603,7 @@ class Generator(FunctionMixin, PyCollector):
raise ValueError("%r generated tests with non-unique name %r" %(self, name))
seen[name] = True
l.append(self.Function(name, self, args=args, callobj=call))
msg = 'yield tests are deprecated, and scheduled to be removed in pytest 4.0'
self.config.warn('C1', msg, fslocation=self.fspath)
self.config.warn('C1', deprecated.YIELD_TESTS, fslocation=self.fspath)
return l
def getcallargs(self, obj):

View File

@ -763,51 +763,3 @@ class TestDurationWithFixture:
* call *test_1*
""")
def test_yield_tests_deprecation(testdir):
testdir.makepyfile("""
def func1(arg, arg2):
assert arg == arg2
def test_gen():
yield "m1", func1, 15, 3*5
yield "m2", func1, 42, 6*7
""")
result = testdir.runpytest('-ra')
result.stdout.fnmatch_lines([
'*yield tests are deprecated, and scheduled to be removed in pytest 4.0*',
'*2 passed*',
])
def test_funcarg_prefix_deprecation(testdir):
testdir.makepyfile("""
def pytest_funcarg__value():
return 10
def test_funcarg_prefix(value):
assert value == 10
""")
result = testdir.runpytest('-ra')
result.stdout.fnmatch_lines([
('WC1 None pytest_funcarg__value: '
'declaring fixtures using "pytest_funcarg__" prefix is deprecated '
'and scheduled to be removed in pytest 4.0. '
'Please remove the prefix and use the @pytest.fixture decorator instead.'),
'*1 passed*',
])
def test_str_args_deprecated(tmpdir, testdir):
"""Deprecate passing strings to pytest.main(). Scheduled for removal in pytest-4.0."""
warnings = []
class Collect:
def pytest_logwarning(self, message):
warnings.append(message)
ret = pytest.main("%s -x" % tmpdir, plugins=[Collect()])
testdir.delete_loaded_modules()
msg = ('passing a string to pytest.main() is deprecated, '
'pass a list of arguments instead.')
assert msg in warnings
assert ret == EXIT_NOTESTSCOLLECTED

View File

@ -0,0 +1,55 @@
import pytest
def test_yield_tests_deprecation(testdir):
testdir.makepyfile("""
def func1(arg, arg2):
assert arg == arg2
def test_gen():
yield "m1", func1, 15, 3*5
yield "m2", func1, 42, 6*7
""")
result = testdir.runpytest('-ra')
result.stdout.fnmatch_lines([
'*yield tests are deprecated, and scheduled to be removed in pytest 4.0*',
'*2 passed*',
])
def test_funcarg_prefix_deprecation(testdir):
testdir.makepyfile("""
def pytest_funcarg__value():
return 10
def test_funcarg_prefix(value):
assert value == 10
""")
result = testdir.runpytest('-ra')
result.stdout.fnmatch_lines([
('WC1 None pytest_funcarg__value: '
'declaring fixtures using "pytest_funcarg__" prefix is deprecated '
'and scheduled to be removed in pytest 4.0. '
'Please remove the prefix and use the @pytest.fixture decorator instead.'),
'*1 passed*',
])
def test_str_args_deprecated(tmpdir, testdir):
"""Deprecate passing strings to pytest.main(). Scheduled for removal in pytest-4.0."""
from _pytest.main import EXIT_NOTESTSCOLLECTED
warnings = []
class Collect:
def pytest_logwarning(self, message):
warnings.append(message)
ret = pytest.main("%s -x" % tmpdir, plugins=[Collect()])
testdir.delete_loaded_modules()
msg = ('passing a string to pytest.main() is deprecated, '
'pass a list of arguments instead.')
assert msg in warnings
assert ret == EXIT_NOTESTSCOLLECTED
def test_getfuncargvalue_is_deprecated(request):
pytest.deprecated_call(request.getfuncargvalue, 'tmpdir')

View File

@ -2909,5 +2909,4 @@ class TestParameterizedSubRequest:
""".format(fixfile.strpath, testfile.basename))
def test_getfuncargvalue_is_deprecated(request):
pytest.deprecated_call(request.getfuncargvalue, 'tmpdir')

View File

@ -29,7 +29,6 @@ def test_funcarg(testdir):
assert bn == "qwe__abc"
def test_ensuretemp(recwarn):
#pytest.deprecated_call(pytest.ensuretemp, 'hello')
d1 = pytest.ensuretemp('hello')
d2 = pytest.ensuretemp('hello')
assert d1 == d2