diff --git a/_pytest/config.py b/_pytest/config.py index f179e5661..7e2163a99 100644 --- a/_pytest/config.py +++ b/_pytest/config.py @@ -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: diff --git a/_pytest/deprecated.py b/_pytest/deprecated.py new file mode 100644 index 000000000..2972c77a5 --- /dev/null +++ b/_pytest/deprecated.py @@ -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" + diff --git a/_pytest/fixtures.py b/_pytest/fixtures.py index 90845df5c..88bcfaa03 100644 --- a/_pytest/fixtures.py +++ b/_pytest/fixtures.py @@ -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) @@ -885,10 +886,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") @@ -1071,7 +1068,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 diff --git a/_pytest/python.py b/_pytest/python.py index d099c6279..9a504f6d8 100644 --- a/_pytest/python.py +++ b/_pytest/python.py @@ -588,6 +588,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 @@ -605,8 +606,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): diff --git a/testing/acceptance_test.py b/testing/acceptance_test.py index 189bdc712..82b131dcc 100644 --- a/testing/acceptance_test.py +++ b/testing/acceptance_test.py @@ -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 diff --git a/testing/deprecated_test.py b/testing/deprecated_test.py new file mode 100644 index 000000000..edbd5d1cd --- /dev/null +++ b/testing/deprecated_test.py @@ -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') diff --git a/testing/python/fixture.py b/testing/python/fixture.py index a16f552c9..efe15ae6b 100644 --- a/testing/python/fixture.py +++ b/testing/python/fixture.py @@ -2941,5 +2941,4 @@ class TestParameterizedSubRequest: """.format(fixfile.strpath, testfile.basename)) -def test_getfuncargvalue_is_deprecated(request): - pytest.deprecated_call(request.getfuncargvalue, 'tmpdir') + diff --git a/testing/test_tmpdir.py b/testing/test_tmpdir.py index d514e722e..232acb6d2 100644 --- a/testing/test_tmpdir.py +++ b/testing/test_tmpdir.py @@ -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