Skip to content

Commit e3facc0

Browse files
authored
Improve clarity of 'expected exception' error messages (#13861)
This PR addresses a TODO in testing/python/raises_group.py that pointed out a poorly structured sentence in a test's expected error message.
1 parent 7939213 commit e3facc0

File tree

4 files changed

+12
-18
lines changed

4 files changed

+12
-18
lines changed

changelog/13861.improvement.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Better sentence structure in a test's expected error message. Previously, the error message would be "expected exception must be <expected>, but got <actual>". Now, it is "Expected <expected>, but got <actual>".

src/_pytest/raises.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -464,11 +464,11 @@ def _parse_exc(
464464
f"with `RaisesGroup`."
465465
)
466466
# unclear if the Type/ValueError distinction is even helpful here
467-
msg = f"expected exception must be {expected}, not "
467+
msg = f"Expected {expected}, but got "
468468
if isinstance(exc, type): # type: ignore[unreachable]
469469
raise ValueError(msg + f"{exc.__name__!r}")
470470
if isinstance(exc, BaseException): # type: ignore[unreachable]
471-
raise TypeError(msg + f"an exception instance ({type(exc).__name__})")
471+
raise TypeError(msg + f"an exception instance: {type(exc).__name__}")
472472
raise TypeError(msg + repr(type(exc).__name__))
473473

474474
@property
@@ -1036,7 +1036,7 @@ def _parse_excgroup(
10361036
return exc
10371037
elif isinstance(exc, tuple):
10381038
raise TypeError(
1039-
f"expected exception must be {expected}, not {type(exc).__name__!r}.\n"
1039+
f"Expected {expected}, but got {type(exc).__name__!r}.\n"
10401040
"RaisesGroup does not support tuples of exception types when expecting one of "
10411041
"several possible exception types like RaisesExc.\n"
10421042
"If you meant to expect a group with multiple exceptions, list them as separate arguments."

testing/python/raises.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -367,9 +367,7 @@ def test_raises_context_manager_with_kwargs(self):
367367
def test_expected_exception_is_not_a_baseexception(self) -> None:
368368
with pytest.raises(
369369
TypeError,
370-
match=wrap_escape(
371-
"expected exception must be a BaseException type, not 'str'"
372-
),
370+
match=wrap_escape("Expected a BaseException type, but got 'str'"),
373371
):
374372
with pytest.raises("hello"): # type: ignore[call-overload]
375373
pass # pragma: no cover
@@ -380,17 +378,15 @@ class NotAnException:
380378
with pytest.raises(
381379
ValueError,
382380
match=wrap_escape(
383-
"expected exception must be a BaseException type, not 'NotAnException'"
381+
"Expected a BaseException type, but got 'NotAnException'"
384382
),
385383
):
386384
with pytest.raises(NotAnException): # type: ignore[type-var]
387385
pass # pragma: no cover
388386

389387
with pytest.raises(
390388
TypeError,
391-
match=wrap_escape(
392-
"expected exception must be a BaseException type, not 'str'"
393-
),
389+
match=wrap_escape("Expected a BaseException type, but got 'str'"),
394390
):
395391
with pytest.raises(("hello", NotAnException)): # type: ignore[arg-type]
396392
pass # pragma: no cover

testing/python/raises_group.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,18 @@ def fails_raises_group(msg: str, add_prefix: bool = True) -> RaisesExc[Failed]:
3636
def test_raises_group() -> None:
3737
with pytest.raises(
3838
TypeError,
39-
match=wrap_escape("expected exception must be a BaseException type, not 'int'"),
39+
match=wrap_escape("Expected a BaseException type, but got 'int'"),
4040
):
4141
RaisesExc(5) # type: ignore[call-overload]
4242
with pytest.raises(
4343
ValueError,
44-
match=wrap_escape("expected exception must be a BaseException type, not 'int'"),
44+
match=wrap_escape("Expected a BaseException type, but got 'int'"),
4545
):
4646
RaisesExc(int) # type: ignore[type-var]
4747
with pytest.raises(
4848
TypeError,
49-
# TODO: bad sentence structure
5049
match=wrap_escape(
51-
"expected exception must be a BaseException type, RaisesExc, or RaisesGroup, not an exception instance (ValueError)",
50+
"Expected a BaseException type, RaisesExc, or RaisesGroup, but got an exception instance: ValueError",
5251
),
5352
):
5453
RaisesGroup(ValueError()) # type: ignore[call-overload]
@@ -1078,9 +1077,7 @@ def test_raisesexc() -> None:
10781077
RaisesExc() # type: ignore[call-overload]
10791078
with pytest.raises(
10801079
ValueError,
1081-
match=wrap_escape(
1082-
"expected exception must be a BaseException type, not 'object'"
1083-
),
1080+
match=wrap_escape("Expected a BaseException type, but got 'object'"),
10841081
):
10851082
RaisesExc(object) # type: ignore[type-var]
10861083

@@ -1351,7 +1348,7 @@ def test_tuples() -> None:
13511348
with pytest.raises(
13521349
TypeError,
13531350
match=wrap_escape(
1354-
"expected exception must be a BaseException type, RaisesExc, or RaisesGroup, not 'tuple'.\n"
1351+
"Expected a BaseException type, RaisesExc, or RaisesGroup, but got 'tuple'.\n"
13551352
"RaisesGroup does not support tuples of exception types when expecting one of "
13561353
"several possible exception types like RaisesExc.\n"
13571354
"If you meant to expect a group with multiple exceptions, list them as separate arguments."

0 commit comments

Comments
 (0)