Skip to content

Commit 9714dc0

Browse files
committed
doc improvements after review
1 parent cb30674 commit 9714dc0

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

doc/en/how-to/assert.rst

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -279,12 +279,12 @@ exception at a specific level; exceptions contained directly in the top
279279
assert not excinfo.group_contains(RuntimeError, depth=2)
280280
assert not excinfo.group_contains(TypeError, depth=1)
281281
282-
Alternate form (legacy)
283-
~~~~~~~~~~~~~~~~~~~~~~~
282+
Alternate `pytest.raises` form (legacy)
283+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
284284

285-
There is an alternate form where you pass
286-
a function that will be executed, along ``*args`` and ``**kwargs``, and :func:`pytest.raises`
287-
will execute the function with the arguments and assert that the given exception is raised:
285+
There is an alternate form of :func:`pytest.raises` where you pass
286+
a function that will be executed, along with ``*args`` and ``**kwargs``. :func:`pytest.raises`
287+
will then execute the function with those arguments and assert that the given exception is raised:
288288

289289
.. code-block:: python
290290
@@ -329,6 +329,18 @@ This will only "xfail" if the test fails by raising ``IndexError`` or subclasses
329329
* Using :func:`pytest.raises` is likely to be better for cases where you are
330330
testing exceptions your own code is deliberately raising, which is the majority of cases.
331331

332+
You can also use :class:`pytest.RaisesGroup`:
333+
334+
.. code-block:: python
335+
336+
def f():
337+
raise ExceptionGroup("", [IndexError()])
338+
339+
340+
@pytest.mark.xfail(raises=RaisesGroup(IndexError))
341+
def test_f():
342+
f()
343+
332344
333345
.. _`assertwarns`:
334346

src/_pytest/_raises_group.py renamed to src/_pytest/raises_group.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -197,9 +197,6 @@ class RaisesExc(AbstractRaises[BaseExcT_co_default]):
197197
...
198198
with RaisesGroups(RaisesExc(check=lambda x: type(x) is ValueError)):
199199
...
200-
201-
Tip: if you install ``hypothesis`` and import it in ``conftest.py`` you will get
202-
readable ``repr``'s of ``check`` callables in the output.
203200
"""
204201

205202
# Trio bundled hypothesis monkeypatching, we will probably instead assume that
@@ -367,8 +364,13 @@ class RaisesGroup(AbstractRaises[BaseExceptionGroup[BaseExcT_co]]):
367364
even though it generally does not care about the order of the exceptions in the group.
368365
To avoid the above you should specify the first :exc:`ValueError` with a :class:`RaisesExc` as well.
369366
370-
Tip: if you install ``hypothesis`` and import it in ``conftest.py`` you will get
371-
readable ``repr``'s of ``check`` callables in the output.
367+
.. note::
368+
When raised exceptions don't match the expected ones, you'll get a detailed error
369+
message explaining why. This includes ``repr(check)`` if set, which in Python can be
370+
overly verbose, showing memory locations etc etc.
371+
372+
If installed and imported (in e.g. ``conftest.py``), the ``hypothesis`` library will
373+
monkeypatch this output to provide shorter & more readable repr's.
372374
"""
373375

374376
# allow_unwrapped=True requires: singular exception, exception not being

0 commit comments

Comments
 (0)