diff --git a/changelog/5180.removal.rst b/changelog/5180.removal.rst index bde3e03ff..2417672be 100644 --- a/changelog/5180.removal.rst +++ b/changelog/5180.removal.rst @@ -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: * ``Request.getfuncargvalue``: use ``Request.getfixturevalue`` instead. * ``pytest.raises`` and ``pytest.warns`` no longer support strings as the second argument. +* ``message`` parameter of ``pytest.raises``. + For more information consult `Deprecations and Removals `__ in the docs. diff --git a/doc/en/deprecations.rst b/doc/en/deprecations.rst index 344e1dd72..c97061f09 100644 --- a/doc/en/deprecations.rst +++ b/doc/en/deprecations.rst @@ -20,8 +20,8 @@ Below is a complete list of all pytest features which are considered deprecated. :ref:`standard warning filters `. -Removal of ``funcargnames`` alias for ``fixturenames`` -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +``funcargnames`` alias for ``fixturenames`` +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .. 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``. -.. _`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 `__. ``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 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 `__. + + .. _raises-warns-exec: ``raises`` / ``warns`` with a string as the second argument diff --git a/src/_pytest/deprecated.py b/src/_pytest/deprecated.py index 77c7c30ff..bbb073520 100644 --- a/src/_pytest/deprecated.py +++ b/src/_pytest/deprecated.py @@ -42,12 +42,6 @@ FUNCARGNAMES = PytestDeprecationWarning( "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 is deprecated and scheduled for removal in pytest 6.0.\n" diff --git a/src/_pytest/python_api.py b/src/_pytest/python_api.py index f709057a3..9f5220766 100644 --- a/src/_pytest/python_api.py +++ b/src/_pytest/python_api.py @@ -1,7 +1,6 @@ import inspect import math import pprint -import warnings from collections.abc import Iterable from collections.abc import Mapping from collections.abc import Sized @@ -12,7 +11,6 @@ from numbers import Number from more_itertools.more import always_iterable import _pytest._code -from _pytest import deprecated from _pytest.compat import STRING_TYPES 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 - :kwparam message: **(deprecated since 4.1)** if specified, provides a custom failure message - if the exception is not raised. See :ref:`the deprecation docs ` for a workaround. .. currentmodule:: _pytest._code @@ -656,9 +652,6 @@ def raises(expected_exception, *args, **kwargs): match_expr = None if not args: - if "message" in kwargs: - message = kwargs.pop("message") - warnings.warn(deprecated.RAISES_MESSAGE_PARAMETER, stacklevel=2) if "match" in kwargs: match_expr = kwargs.pop("match") if kwargs: diff --git a/testing/deprecated_test.py b/testing/deprecated_test.py index 4ce89113d..4db4a9c98 100644 --- a/testing/deprecated_test.py +++ b/testing/deprecated_test.py @@ -76,12 +76,6 @@ def test_external_plugins_integrated(testdir, 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): from _pytest.deprecated import PYTEST_PLUGINS_FROM_NON_TOP_LEVEL_CONFTEST diff --git a/testing/python/raises.py b/testing/python/raises.py index 3ace96e7a..4f514e322 100644 --- a/testing/python/raises.py +++ b/testing/python/raises.py @@ -2,7 +2,6 @@ import sys import pytest from _pytest.outcomes import Failed -from _pytest.warning_types import PytestDeprecationWarning class TestRaises: @@ -155,17 +154,6 @@ class TestRaises: else: 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"]) def test_raises_cyclic_reference(self, method): """