From 8a1afe421357439dd15d9a16448ecbdd69fdab85 Mon Sep 17 00:00:00 2001 From: Arel Cordero Date: Mon, 28 Jan 2019 13:31:08 +0000 Subject: [PATCH] Including note on using nullcontext in Python 3.7+ --- doc/en/example/parametrize.rst | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/doc/en/example/parametrize.rst b/doc/en/example/parametrize.rst index b7be543ea..0a16621d3 100644 --- a/doc/en/example/parametrize.rst +++ b/doc/en/example/parametrize.rst @@ -569,7 +569,7 @@ Use :func:`pytest.raises` with the :ref:`pytest.mark.parametrize ref` decorator to write parametrized tests in which some tests raise exceptions and others do not. -It is helpful to define a function such as ``does_not_raise`` to serve +It is helpful to define a context manager ``does_not_raise`` to serve as a complement to ``raises``. For example:: from contextlib import contextmanager @@ -591,5 +591,10 @@ as a complement to ``raises``. For example:: with expectation: assert (6 / example_input) is not None -In this example, the first three test cases should run unexceptionally, +In the example above, the first three test cases should run unexceptionally, while the fourth should raise ``ZeroDivisionError``. + +In Python 3.7+, you can simply use ``nullcontext`` to define +``does_not_raise``:: + + from contextlib import nullcontext as does_not_raise