Skip to content

Commit e0e9af2

Browse files
committed
Deprecate support for passing command-line as string to pytest.main()
Fixes #1723
1 parent 1fb09d9 commit e0e9af2

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

CHANGELOG.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,9 @@ time or change existing behaviors in order to make them less surprising/more use
203203
removed in pytest-4.0 (`#1684`_).
204204
Thanks `@nicoddemus`_ for the PR.
205205

206+
* Passing a command-line string to ``pytest.main()`` is considered deprecated and scheduled
207+
for removal in pytest-4.0. It is recommended to pass a list of arguments instead (`#1723`_).
208+
206209
* Rename ``getfuncargvalue`` to ``getfixturevalue``. ``getfuncargvalue`` is
207210
still present but is now considered deprecated. Thanks to `@RedBeardCode`_ and `@tomviner`_
208211
for the PR (`#1626`_).
@@ -282,6 +285,7 @@ time or change existing behaviors in order to make them less surprising/more use
282285
.. _#1633: https://github.com/pytest-dev/pytest/pull/1633
283286
.. _#1664: https://github.com/pytest-dev/pytest/pull/1664
284287
.. _#1684: https://github.com/pytest-dev/pytest/pull/1684
288+
.. _#1723: https://github.com/pytest-dev/pytest/pull/1723
285289

286290
.. _@DRMacIver: https://github.com/DRMacIver
287291
.. _@RedBeardCode: https://github.com/RedBeardCode

_pytest/config.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ def get_plugin_manager():
9898
return get_config().pluginmanager
9999

100100
def _prepareconfig(args=None, plugins=None):
101+
warning = None
101102
if args is None:
102103
args = sys.argv[1:]
103104
elif isinstance(args, py.path.local):
@@ -106,6 +107,10 @@ def _prepareconfig(args=None, plugins=None):
106107
if not isinstance(args, str):
107108
raise ValueError("not a string or argument list: %r" % (args,))
108109
args = shlex.split(args, posix=sys.platform != "win32")
110+
# we want to remove this way of passing arguments to pytest.main()
111+
# in pytest-4.0
112+
warning = 'passing a string to pytest.main() is deprecated, ' \
113+
'pass a list of arguments instead.'
109114
config = get_config()
110115
pluginmanager = config.pluginmanager
111116
try:
@@ -115,6 +120,8 @@ def _prepareconfig(args=None, plugins=None):
115120
pluginmanager.consider_pluginarg(plugin)
116121
else:
117122
pluginmanager.register(plugin)
123+
if warning:
124+
config.warn('C1', warning)
118125
return pluginmanager.hook.pytest_cmdline_parse(
119126
pluginmanager=pluginmanager, args=args)
120127
except BaseException:

testing/acceptance_test.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -795,3 +795,17 @@ def test_funcarg_prefix(value):
795795
'Please remove the prefix and use the @pytest.fixture decorator instead.'),
796796
'*1 passed*',
797797
])
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+
assert warnings == ['passing a string to pytest.main() is deprecated, pass a list of arguments instead.']
811+
assert ret == EXIT_NOTESTSCOLLECTED

0 commit comments

Comments
 (0)