Remove 'message' parameter from pytest.raises
This commit is contained in:
parent
279733a30b
commit
13f7f27fd2
|
@ -1,10 +1,12 @@
|
||||||
As per our policy, the following features have been deprecated in the 4.X series and are now being
|
As per our policy, the following features have been deprecated in the 4.X series and are now
|
||||||
removed:
|
removed:
|
||||||
|
|
||||||
* ``Request.getfuncargvalue``: use ``Request.getfixturevalue`` instead.
|
* ``Request.getfuncargvalue``: use ``Request.getfixturevalue`` instead.
|
||||||
|
|
||||||
* ``pytest.raises`` and ``pytest.warns`` no longer support strings as the second argument.
|
* ``pytest.raises`` and ``pytest.warns`` no longer support strings as the second argument.
|
||||||
|
|
||||||
|
* ``message`` parameter of ``pytest.raises``.
|
||||||
|
|
||||||
|
|
||||||
For more information consult
|
For more information consult
|
||||||
`Deprecations and Removals <https://docs.pytest.org/en/latest/deprecations.html>`__ in the docs.
|
`Deprecations and Removals <https://docs.pytest.org/en/latest/deprecations.html>`__ in the docs.
|
||||||
|
|
|
@ -20,8 +20,8 @@ Below is a complete list of all pytest features which are considered deprecated.
|
||||||
:ref:`standard warning filters <warnings>`.
|
:ref:`standard warning filters <warnings>`.
|
||||||
|
|
||||||
|
|
||||||
Removal of ``funcargnames`` alias for ``fixturenames``
|
``funcargnames`` alias for ``fixturenames``
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
.. deprecated:: 5.0
|
.. deprecated:: 5.0
|
||||||
|
|
||||||
|
@ -34,40 +34,6 @@ in places where we or plugin authors must distinguish between fixture names and
|
||||||
names supplied by non-fixture things such as ``pytest.mark.parametrize``.
|
names supplied by non-fixture things such as ``pytest.mark.parametrize``.
|
||||||
|
|
||||||
|
|
||||||
.. _`raises message deprecated`:
|
|
||||||
|
|
||||||
``"message"`` parameter of ``pytest.raises``
|
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
||||||
|
|
||||||
.. deprecated:: 4.1
|
|
||||||
|
|
||||||
It is a common mistake to think this parameter will match the exception message, while in fact
|
|
||||||
it only serves to provide a custom message in case the ``pytest.raises`` check fails. To prevent
|
|
||||||
users from making this mistake, and because it is believed to be little used, pytest is
|
|
||||||
deprecating it without providing an alternative for the moment.
|
|
||||||
|
|
||||||
If you have a valid use case for this parameter, consider that to obtain the same results
|
|
||||||
you can just call ``pytest.fail`` manually at the end of the ``with`` statement.
|
|
||||||
|
|
||||||
For example:
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
with pytest.raises(TimeoutError, message="Client got unexpected message"):
|
|
||||||
wait_for(websocket.recv(), 0.5)
|
|
||||||
|
|
||||||
|
|
||||||
Becomes:
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
with pytest.raises(TimeoutError):
|
|
||||||
wait_for(websocket.recv(), 0.5)
|
|
||||||
pytest.fail("Client got unexpected message")
|
|
||||||
|
|
||||||
|
|
||||||
If you still have concerns about this deprecation and future removal, please comment on
|
|
||||||
`issue #3974 <https://github.com/pytest-dev/pytest/issues/3974>`__.
|
|
||||||
|
|
||||||
|
|
||||||
``pytest.config`` global
|
``pytest.config`` global
|
||||||
|
@ -103,6 +69,43 @@ Removed Features
|
||||||
As stated in our :ref:`backwards-compatibility` policy, deprecated features are removed only in major releases after
|
As stated in our :ref:`backwards-compatibility` policy, deprecated features are removed only in major releases after
|
||||||
an appropriate period of deprecation has passed.
|
an appropriate period of deprecation has passed.
|
||||||
|
|
||||||
|
|
||||||
|
.. _`raises message deprecated`:
|
||||||
|
|
||||||
|
``"message"`` parameter of ``pytest.raises``
|
||||||
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
.. versionremoved:: 5.0
|
||||||
|
|
||||||
|
It is a common mistake to think this parameter will match the exception message, while in fact
|
||||||
|
it only serves to provide a custom message in case the ``pytest.raises`` check fails. To prevent
|
||||||
|
users from making this mistake, and because it is believed to be little used, pytest is
|
||||||
|
deprecating it without providing an alternative for the moment.
|
||||||
|
|
||||||
|
If you have a valid use case for this parameter, consider that to obtain the same results
|
||||||
|
you can just call ``pytest.fail`` manually at the end of the ``with`` statement.
|
||||||
|
|
||||||
|
For example:
|
||||||
|
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
|
with pytest.raises(TimeoutError, message="Client got unexpected message"):
|
||||||
|
wait_for(websocket.recv(), 0.5)
|
||||||
|
|
||||||
|
|
||||||
|
Becomes:
|
||||||
|
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
|
with pytest.raises(TimeoutError):
|
||||||
|
wait_for(websocket.recv(), 0.5)
|
||||||
|
pytest.fail("Client got unexpected message")
|
||||||
|
|
||||||
|
|
||||||
|
If you still have concerns about this deprecation and future removal, please comment on
|
||||||
|
`issue #3974 <https://github.com/pytest-dev/pytest/issues/3974>`__.
|
||||||
|
|
||||||
|
|
||||||
.. _raises-warns-exec:
|
.. _raises-warns-exec:
|
||||||
|
|
||||||
``raises`` / ``warns`` with a string as the second argument
|
``raises`` / ``warns`` with a string as the second argument
|
||||||
|
|
|
@ -42,12 +42,6 @@ FUNCARGNAMES = PytestDeprecationWarning(
|
||||||
"since pytest 2.3 - use the newer attribute instead."
|
"since pytest 2.3 - use the newer attribute instead."
|
||||||
)
|
)
|
||||||
|
|
||||||
RAISES_MESSAGE_PARAMETER = PytestDeprecationWarning(
|
|
||||||
"The 'message' parameter is deprecated.\n"
|
|
||||||
"(did you mean to use `match='some regex'` to check the exception message?)\n"
|
|
||||||
"Please see:\n"
|
|
||||||
" https://docs.pytest.org/en/4.6-maintenance/deprecations.html#message-parameter-of-pytest-raises"
|
|
||||||
)
|
|
||||||
|
|
||||||
RESULT_LOG = PytestDeprecationWarning(
|
RESULT_LOG = PytestDeprecationWarning(
|
||||||
"--result-log is deprecated and scheduled for removal in pytest 6.0.\n"
|
"--result-log is deprecated and scheduled for removal in pytest 6.0.\n"
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
import inspect
|
import inspect
|
||||||
import math
|
import math
|
||||||
import pprint
|
import pprint
|
||||||
import warnings
|
|
||||||
from collections.abc import Iterable
|
from collections.abc import Iterable
|
||||||
from collections.abc import Mapping
|
from collections.abc import Mapping
|
||||||
from collections.abc import Sized
|
from collections.abc import Sized
|
||||||
|
@ -12,7 +11,6 @@ from numbers import Number
|
||||||
from more_itertools.more import always_iterable
|
from more_itertools.more import always_iterable
|
||||||
|
|
||||||
import _pytest._code
|
import _pytest._code
|
||||||
from _pytest import deprecated
|
|
||||||
from _pytest.compat import STRING_TYPES
|
from _pytest.compat import STRING_TYPES
|
||||||
from _pytest.outcomes import fail
|
from _pytest.outcomes import fail
|
||||||
|
|
||||||
|
@ -538,8 +536,6 @@ def raises(expected_exception, *args, **kwargs):
|
||||||
|
|
||||||
__ https://docs.python.org/3/library/re.html#regular-expression-syntax
|
__ https://docs.python.org/3/library/re.html#regular-expression-syntax
|
||||||
|
|
||||||
:kwparam message: **(deprecated since 4.1)** if specified, provides a custom failure message
|
|
||||||
if the exception is not raised. See :ref:`the deprecation docs <raises message deprecated>` for a workaround.
|
|
||||||
|
|
||||||
.. currentmodule:: _pytest._code
|
.. currentmodule:: _pytest._code
|
||||||
|
|
||||||
|
@ -656,9 +652,6 @@ def raises(expected_exception, *args, **kwargs):
|
||||||
match_expr = None
|
match_expr = None
|
||||||
|
|
||||||
if not args:
|
if not args:
|
||||||
if "message" in kwargs:
|
|
||||||
message = kwargs.pop("message")
|
|
||||||
warnings.warn(deprecated.RAISES_MESSAGE_PARAMETER, stacklevel=2)
|
|
||||||
if "match" in kwargs:
|
if "match" in kwargs:
|
||||||
match_expr = kwargs.pop("match")
|
match_expr = kwargs.pop("match")
|
||||||
if kwargs:
|
if kwargs:
|
||||||
|
|
|
@ -76,12 +76,6 @@ def test_external_plugins_integrated(testdir, plugin):
|
||||||
testdir.parseconfig("-p", plugin)
|
testdir.parseconfig("-p", plugin)
|
||||||
|
|
||||||
|
|
||||||
def test_raises_message_argument_deprecated():
|
|
||||||
with pytest.warns(pytest.PytestDeprecationWarning):
|
|
||||||
with pytest.raises(RuntimeError, message="foobar"):
|
|
||||||
raise RuntimeError
|
|
||||||
|
|
||||||
|
|
||||||
def test_pytest_plugins_in_non_top_level_conftest_deprecated(testdir):
|
def test_pytest_plugins_in_non_top_level_conftest_deprecated(testdir):
|
||||||
from _pytest.deprecated import PYTEST_PLUGINS_FROM_NON_TOP_LEVEL_CONFTEST
|
from _pytest.deprecated import PYTEST_PLUGINS_FROM_NON_TOP_LEVEL_CONFTEST
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,6 @@ import sys
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
from _pytest.outcomes import Failed
|
from _pytest.outcomes import Failed
|
||||||
from _pytest.warning_types import PytestDeprecationWarning
|
|
||||||
|
|
||||||
|
|
||||||
class TestRaises:
|
class TestRaises:
|
||||||
|
@ -155,17 +154,6 @@ class TestRaises:
|
||||||
else:
|
else:
|
||||||
assert False, "Expected pytest.raises.Exception"
|
assert False, "Expected pytest.raises.Exception"
|
||||||
|
|
||||||
def test_custom_raise_message(self):
|
|
||||||
message = "TEST_MESSAGE"
|
|
||||||
try:
|
|
||||||
with pytest.warns(PytestDeprecationWarning):
|
|
||||||
with pytest.raises(ValueError, message=message):
|
|
||||||
pass
|
|
||||||
except pytest.raises.Exception as e:
|
|
||||||
assert e.msg == message
|
|
||||||
else:
|
|
||||||
assert False, "Expected pytest.raises.Exception"
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("method", ["function", "with"])
|
@pytest.mark.parametrize("method", ["function", "with"])
|
||||||
def test_raises_cyclic_reference(self, method):
|
def test_raises_cyclic_reference(self, method):
|
||||||
"""
|
"""
|
||||||
|
|
Loading…
Reference in New Issue