From 21f45ac5f3542b2b80124e7f7ae4af61281cea26 Mon Sep 17 00:00:00 2001 From: Christoph Anton Mitterer Date: Thu, 3 Aug 2023 16:14:56 +0200 Subject: [PATCH 1/3] doc: don't use different name Needlessly using a different name rather just confuses people. Signed-off-by: Christoph Anton Mitterer --- doc/en/example/parametrize.rst | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/doc/en/example/parametrize.rst b/doc/en/example/parametrize.rst index f771e5da4e0..ff64ce077da 100644 --- a/doc/en/example/parametrize.rst +++ b/doc/en/example/parametrize.rst @@ -663,7 +663,7 @@ For example: .. code-block:: python - from contextlib import nullcontext as does_not_raise + from contextlib import nullcontext import pytest @@ -671,9 +671,9 @@ For example: @pytest.mark.parametrize( "example_input,expectation", [ - (3, does_not_raise()), - (2, does_not_raise()), - (1, does_not_raise()), + (3, nullcontext()), + (2, nullcontext()), + (1, nullcontext()), (0, pytest.raises(ZeroDivisionError)), ], ) From 672e9a35bf5278c1e922e4abc4c1594090a9acad Mon Sep 17 00:00:00 2001 From: Christoph Anton Mitterer Date: Thu, 3 Aug 2023 16:23:09 +0200 Subject: [PATCH 2/3] doc: improve parametrizing conditional raising example to also check for values What one typically actually wants in such a case is both, checking for some resulting values *and* checking for some expected exception. Since this is easily possible with the `nullcontext` context manager, adapt the example accordingly. Signed-off-by: Christoph Anton Mitterer --- doc/en/example/parametrize.rst | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/doc/en/example/parametrize.rst b/doc/en/example/parametrize.rst index ff64ce077da..1c940f4234a 100644 --- a/doc/en/example/parametrize.rst +++ b/doc/en/example/parametrize.rst @@ -657,7 +657,10 @@ 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 may be helpful to use ``nullcontext`` as a complement to ``raises``. +``contextlib.nullcontext`` can be used to test cases that are not expected to +raise exceptions but that should result in some value. The value is given as the +``enter_result`` parameter, which will be available as the ``with`` statement’s +target (``e`` in the example below). For example: @@ -671,16 +674,16 @@ For example: @pytest.mark.parametrize( "example_input,expectation", [ - (3, nullcontext()), - (2, nullcontext()), - (1, nullcontext()), + (3, nullcontext(2)), + (2, nullcontext(3)), + (1, nullcontext(6)), (0, pytest.raises(ZeroDivisionError)), ], ) def test_division(example_input, expectation): """Test how much I know division.""" - with expectation: - assert (6 / example_input) is not None + with expectation as e: + assert (6 / example_input) == e In the example above, the first three test cases should run unexceptionally, while the fourth should raise ``ZeroDivisionError``. From 1cc5b113aca26340ea260afd58f81a8b1fd46466 Mon Sep 17 00:00:00 2001 From: Christoph Anton Mitterer Date: Thu, 3 Aug 2023 16:27:33 +0200 Subject: [PATCH 3/3] doc: further minor improvements to parametrizing conditional raising Signed-off-by: Christoph Anton Mitterer --- doc/en/example/parametrize.rst | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/doc/en/example/parametrize.rst b/doc/en/example/parametrize.rst index 1c940f4234a..4ea6f6e6512 100644 --- a/doc/en/example/parametrize.rst +++ b/doc/en/example/parametrize.rst @@ -685,5 +685,6 @@ For example: with expectation as e: assert (6 / example_input) == e -In the example above, the first three test cases should run unexceptionally, -while the fourth should raise ``ZeroDivisionError``. +In the example above, the first three test cases should run without any +exceptions, while the fourth should raise a``ZeroDivisionError`` exception, +which is expected by pytest.