Skip to content

Commit 9741a8c

Browse files
committed
Deprecate the 'message' parameter of pytest.raises
Fix #3974
1 parent 110fe24 commit 9741a8c

File tree

7 files changed

+41
-5
lines changed

7 files changed

+41
-5
lines changed

changelog/3974.deprecation.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Passing the ``message`` parameter of ``pytest.raises`` now issues a ``DeprecationWarning``.
2+
3+
It is a common mistake to think this parameter will match the exception message, while in fact
4+
it only serves to provide a custom message in case the ``pytest.raises`` check fails. To avoid this
5+
mistake and because it is believed to be little used, pytest is deprecating it without providing
6+
an alternative for the moment.
7+
8+
If you have concerns about this, please comment on `issue #3974 <https://github.com/pytest-dev/pytest/issues/3974>`__.

doc/en/deprecations.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,19 @@ Below is a complete list of all pytest features which are considered deprecated.
1414
:class:`_pytest.warning_types.PytestWarning` or subclasses, which can be filtered using
1515
:ref:`standard warning filters <warnings>`.
1616

17+
``"message"`` parameter of ``pytest.raises``
18+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
19+
20+
.. deprecated:: 4.1
21+
22+
It is a common mistake to think this parameter will match the exception message, while in fact
23+
it only serves to provide a custom message in case the ``pytest.raises`` check fails. To avoid this
24+
mistake and because it is believed to be little used, pytest is deprecating it without providing
25+
an alternative for the moment.
26+
27+
If you have concerns about this, please comment on `issue #3974 <https://github.com/pytest-dev/pytest/issues/3974>`__.
28+
29+
1730
``pytest.config`` global
1831
~~~~~~~~~~~~~~~~~~~~~~~~
1932

src/_pytest/deprecated.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,13 @@
5151
"getfuncargvalue is deprecated, use getfixturevalue"
5252
)
5353

54+
RAISES_MESSAGE_PARAMETER = PytestDeprecationWarning(
55+
"The 'message' parameter is deprecated.\n"
56+
"(did you mean to use `match='some regex'` to check the exception message?)\n"
57+
"Please comment on https://github.com/pytest-dev/pytest/issues/3974 "
58+
"if you have concerns about removal of this parameter."
59+
)
60+
5461
RESULT_LOG = PytestDeprecationWarning(
5562
"--result-log is deprecated and scheduled for removal in pytest 5.0.\n"
5663
"See https://docs.pytest.org/en/latest/deprecations.html#result-log-result-log for more information."

src/_pytest/python_api.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313
from six.moves import zip
1414

1515
import _pytest._code
16+
from _pytest import deprecated
1617
from _pytest.compat import isclass
1718
from _pytest.compat import Mapping
1819
from _pytest.compat import Sequence
1920
from _pytest.compat import STRING_TYPES
20-
from _pytest.deprecated import RAISES_EXEC
2121
from _pytest.outcomes import fail
2222

2323
BASE_TYPE = (type, STRING_TYPES)
@@ -660,6 +660,7 @@ def raises(expected_exception, *args, **kwargs):
660660
if not args:
661661
if "message" in kwargs:
662662
message = kwargs.pop("message")
663+
warnings.warn(deprecated.RAISES_MESSAGE_PARAMETER, stacklevel=2)
663664
if "match" in kwargs:
664665
match_expr = kwargs.pop("match")
665666
if kwargs:
@@ -668,7 +669,7 @@ def raises(expected_exception, *args, **kwargs):
668669
raise TypeError(msg)
669670
return RaisesContext(expected_exception, message, match_expr)
670671
elif isinstance(args[0], str):
671-
warnings.warn(RAISES_EXEC, stacklevel=2)
672+
warnings.warn(deprecated.RAISES_EXEC, stacklevel=2)
672673
code, = args
673674
assert isinstance(code, str)
674675
frame = sys._getframe(1)

testing/deprecated_test.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,12 @@ def test_func(pytestconfig):
136136
)
137137

138138

139+
def test_raises_message_argument_deprecated():
140+
with pytest.warns(pytest.PytestDeprecationWarning):
141+
with pytest.raises(RuntimeError, message="foobar"):
142+
raise RuntimeError
143+
144+
139145
def test_pytest_plugins_in_non_top_level_conftest_deprecated(testdir):
140146
from _pytest.deprecated import PYTEST_PLUGINS_FROM_NON_TOP_LEVEL_CONFTEST
141147

testing/python/raises.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,9 @@ def test_no_raise_message(self):
121121
def test_custom_raise_message(self):
122122
message = "TEST_MESSAGE"
123123
try:
124-
with pytest.raises(ValueError, message=message):
125-
pass
124+
with pytest.warns(PytestDeprecationWarning):
125+
with pytest.raises(ValueError, message=message):
126+
pass
126127
except pytest.raises.Exception as e:
127128
assert e.msg == message
128129
else:

testing/test_pytester.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ def test_assert_outcomes_after_pytest_error(testdir):
280280
testdir.makepyfile("def test_foo(): assert True")
281281

282282
result = testdir.runpytest("--unexpected-argument")
283-
with pytest.raises(ValueError, message="Pytest terminal report not found"):
283+
with pytest.raises(ValueError):
284284
result.assert_outcomes(passed=0)
285285

286286

0 commit comments

Comments
 (0)