File tree Expand file tree Collapse file tree 1 file changed +22
-2
lines changed Expand file tree Collapse file tree 1 file changed +22
-2
lines changed Original file line number Diff line number Diff line change @@ -233,7 +233,7 @@ You can also use it as a contextmanager::
233
233
.. _warns :
234
234
235
235
Asserting warnings with the warns function
236
- -----------------------------------------------
236
+ ------------------------------------------
237
237
238
238
.. versionadded :: 2.8
239
239
@@ -291,7 +291,7 @@ Alternatively, you can examine raised warnings in detail using the
291
291
.. _recwarn :
292
292
293
293
Recording warnings
294
- ------------------------
294
+ ------------------
295
295
296
296
You can record raised warnings either using ``pytest.warns `` or with
297
297
the ``recwarn `` fixture.
@@ -329,6 +329,26 @@ warnings, or index into it to get a particular recorded warning.
329
329
330
330
Full API: :class: `WarningsRecorder `.
331
331
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.
332
352
333
353
.. _internal-warnings :
334
354
You can’t perform that action at this time.
0 commit comments