Skip to content

Commit

Permalink
Remove support to pass strings to pytest.main()
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoddemus committed Dec 14, 2018
1 parent 6e1b1ab commit ba8c12d
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 51 deletions.
3 changes: 3 additions & 0 deletions changelog/3085.removal.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Removed support for passing strings to ``pytest.main``. Now, always pass a list of strings instead.

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.
45 changes: 24 additions & 21 deletions doc/en/deprecations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -193,27 +193,6 @@ To update the code, use ``pytest.param``:
Passing command-line string to ``pytest.main()``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. deprecated:: 3.0

Passing a command-line string to ``pytest.main()`` is deprecated:

.. code-block:: python
pytest.main("-v -s")
Pass a list instead:

.. code-block:: python
pytest.main(["-v", "-s"])
By passing a string, users expect that pytest will interpret that command-line using the shell rules they are working
on (for example ``bash`` or ``Powershell``), but this is very hard/impossible to do in a portable way.

[pytest] section in setup.cfg files
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down Expand Up @@ -353,6 +332,30 @@ Change to:
def test_foo(record_property):
...
Passing command-line string to ``pytest.main()``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

*Removed in version 4.0.*

Passing a command-line string to ``pytest.main()`` is deprecated:

.. code-block:: python
pytest.main("-v -s")
Pass a list instead:

.. code-block:: python
pytest.main(["-v", "-s"])
By passing a string, users expect that pytest will interpret that command-line using the shell rules they are working
on (for example ``bash`` or ``Powershell``), but this is very hard/impossible to do in a portable way.



``yield`` tests
~~~~~~~~~~~~~~~

Expand Down
7 changes: 2 additions & 5 deletions src/_pytest/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,9 @@ def _prepareconfig(args=None, plugins=None):
elif isinstance(args, py.path.local):
args = [str(args)]
elif not isinstance(args, (tuple, list)):
if not isinstance(args, str):
raise ValueError("not a string or argument list: %r" % (args,))
args = shlex.split(args, posix=sys.platform != "win32")
from _pytest import deprecated
msg = "`args` parameter expected to be a list or tuple of strings, got: {!r} (type: {})"
raise TypeError(msg.format(args, type(args)))

warning = deprecated.MAIN_STR_ARGS
config = get_config()
pluginmanager = config.pluginmanager
try:
Expand Down
11 changes: 5 additions & 6 deletions testing/acceptance_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -559,12 +559,11 @@ def test_python_pytest_package(self, testdir):
def test_equivalence_pytest_pytest(self):
assert pytest.main == py.test.cmdline.main

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

def test_invoke_with_path(self, tmpdir, capsys):
retcode = pytest.main(tmpdir)
Expand Down
19 changes: 0 additions & 19 deletions testing/deprecated_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,25 +40,6 @@ def test_pytest_custom_cfg_deprecated(testdir):
)


def test_str_args_deprecated(tmpdir):
"""Deprecate passing strings to pytest.main(). Scheduled for removal in pytest-4.0."""
from _pytest.main import EXIT_NOTESTSCOLLECTED

warnings = []

class Collect(object):
def pytest_warning_captured(self, warning_message):
warnings.append(str(warning_message.message))

ret = pytest.main("%s -x" % tmpdir, plugins=[Collect()])
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")

Expand Down

0 comments on commit ba8c12d

Please sign in to comment.