Skip to content

Commit f0ecb25

Browse files
committed
Document custom failure messages for missing warnings
1 parent 7dbe400 commit f0ecb25

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

doc/en/warnings.rst

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ You can also use it as a contextmanager::
233233
.. _warns:
234234

235235
Asserting warnings with the warns function
236-
-----------------------------------------------
236+
------------------------------------------
237237

238238
.. versionadded:: 2.8
239239

@@ -291,7 +291,7 @@ Alternatively, you can examine raised warnings in detail using the
291291
.. _recwarn:
292292

293293
Recording warnings
294-
------------------------
294+
------------------
295295

296296
You can record raised warnings either using ``pytest.warns`` or with
297297
the ``recwarn`` fixture.
@@ -329,6 +329,26 @@ warnings, or index into it to get a particular recorded warning.
329329

330330
Full API: :class:`WarningsRecorder`.
331331

332+
.. _custom_failure_messages:
333+
334+
Custom failure messages
335+
-----------------------
336+
337+
Recording warnings provides an opportunity to produce custom test
338+
failure messages for when no warnings are issued or other conditions
339+
are met.
340+
341+
.. code-block:: python
342+
343+
def test():
344+
with pytest.warns(Warning) as record:
345+
f()
346+
if not record:
347+
pytest.fail('Expected a warning!')
348+
349+
If no warnings are issued when calling ``f``, then ``not record`` will
350+
evaluate to ``False``. You can then call ``pytest.fail`` with a
351+
custom error message.
332352

333353
.. _internal-warnings:
334354

0 commit comments

Comments
 (0)