Skip to content

Commit a7e4016

Browse files
committed
Remove support to pass strings to pytest.main()
Fix pytest-dev#3085
1 parent 6e1b1ab commit a7e4016

File tree

6 files changed

+34
-56
lines changed

6 files changed

+34
-56
lines changed

changelog/3085.removal.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Removed support for passing strings to ``pytest.main``. Now, always pass a list of strings instead.
2+
3+
See our `docs <https://docs.pytest.org/en/latest/deprecations.html#passing-command-line-string-to-pytest-main>`__ on information on how to update your code.

doc/en/deprecations.rst

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -193,27 +193,6 @@ To update the code, use ``pytest.param``:
193193
194194
195195
196-
Passing command-line string to ``pytest.main()``
197-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
198-
199-
.. deprecated:: 3.0
200-
201-
Passing a command-line string to ``pytest.main()`` is deprecated:
202-
203-
.. code-block:: python
204-
205-
pytest.main("-v -s")
206-
207-
Pass a list instead:
208-
209-
.. code-block:: python
210-
211-
pytest.main(["-v", "-s"])
212-
213-
214-
By passing a string, users expect that pytest will interpret that command-line using the shell rules they are working
215-
on (for example ``bash`` or ``Powershell``), but this is very hard/impossible to do in a portable way.
216-
217196
218197
[pytest] section in setup.cfg files
219198
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -353,6 +332,30 @@ Change to:
353332
def test_foo(record_property):
354333
...
355334
335+
336+
Passing command-line string to ``pytest.main()``
337+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
338+
339+
*Removed in version 4.0.*
340+
341+
Passing a command-line string to ``pytest.main()`` is deprecated:
342+
343+
.. code-block:: python
344+
345+
pytest.main("-v -s")
346+
347+
Pass a list instead:
348+
349+
.. code-block:: python
350+
351+
pytest.main(["-v", "-s"])
352+
353+
354+
By passing a string, users expect that pytest will interpret that command-line using the shell rules they are working
355+
on (for example ``bash`` or ``Powershell``), but this is very hard/impossible to do in a portable way.
356+
357+
358+
356359
``yield`` tests
357360
~~~~~~~~~~~~~~~
358361

src/_pytest/config/__init__.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -173,12 +173,9 @@ def _prepareconfig(args=None, plugins=None):
173173
elif isinstance(args, py.path.local):
174174
args = [str(args)]
175175
elif not isinstance(args, (tuple, list)):
176-
if not isinstance(args, str):
177-
raise ValueError("not a string or argument list: %r" % (args,))
178-
args = shlex.split(args, posix=sys.platform != "win32")
179-
from _pytest import deprecated
176+
msg = "`args` parameter expected to be a list or tuple of strings, got: {!r} (type: {})"
177+
raise TypeError(msg.format(args, type(args)))
180178

181-
warning = deprecated.MAIN_STR_ARGS
182179
config = get_config()
183180
pluginmanager = config.pluginmanager
184181
try:

src/_pytest/deprecated.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,6 @@
1717
from _pytest.warning_types import UnformattedWarning
1818

1919

20-
MAIN_STR_ARGS = RemovedInPytest4Warning(
21-
"passing a string to pytest.main() is deprecated, "
22-
"pass a list of arguments instead."
23-
)
24-
2520
YIELD_TESTS = "yield tests were removed in pytest 4.0 - {name} will be ignored"
2621

2722

testing/acceptance_test.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -559,12 +559,11 @@ def test_python_pytest_package(self, testdir):
559559
def test_equivalence_pytest_pytest(self):
560560
assert pytest.main == py.test.cmdline.main
561561

562-
def test_invoke_with_string(self, capsys):
563-
retcode = pytest.main("-h")
564-
assert not retcode
565-
out, err = capsys.readouterr()
566-
assert "--help" in out
567-
pytest.raises(ValueError, lambda: pytest.main(0))
562+
def test_invoke_with_invalid_type(self, capsys):
563+
with pytest.raises(
564+
TypeError, match="expected to be a list or tuple of strings, got: '-h'"
565+
):
566+
pytest.main("-h")
568567

569568
def test_invoke_with_path(self, tmpdir, capsys):
570569
retcode = pytest.main(tmpdir)

testing/deprecated_test.py

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -40,25 +40,6 @@ def test_pytest_custom_cfg_deprecated(testdir):
4040
)
4141

4242

43-
def test_str_args_deprecated(tmpdir):
44-
"""Deprecate passing strings to pytest.main(). Scheduled for removal in pytest-4.0."""
45-
from _pytest.main import EXIT_NOTESTSCOLLECTED
46-
47-
warnings = []
48-
49-
class Collect(object):
50-
def pytest_warning_captured(self, warning_message):
51-
warnings.append(str(warning_message.message))
52-
53-
ret = pytest.main("%s -x" % tmpdir, plugins=[Collect()])
54-
msg = (
55-
"passing a string to pytest.main() is deprecated, "
56-
"pass a list of arguments instead."
57-
)
58-
assert msg in warnings
59-
assert ret == EXIT_NOTESTSCOLLECTED
60-
61-
6243
def test_getfuncargvalue_is_deprecated(request):
6344
pytest.deprecated_call(request.getfuncargvalue, "tmpdir")
6445

0 commit comments

Comments
 (0)