From 550a42d71727522bcbd88657aa3ea9e035948cc4 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Wed, 29 Jun 2022 14:00:03 -0400 Subject: [PATCH] [7.1.x] update does_not_raise docs now that pytest is 3.7+ only (cherry picked from commit 2941da0f2bdc58cabfc6977f45bea3c9404493ea) --- doc/en/example/parametrize.rst | 32 +++++--------------------------- testing/python/raises.py | 12 ++---------- 2 files changed, 7 insertions(+), 37 deletions(-) diff --git a/doc/en/example/parametrize.rst b/doc/en/example/parametrize.rst index 66d72f3cc..f81476839 100644 --- a/doc/en/example/parametrize.rst +++ b/doc/en/example/parametrize.rst @@ -657,20 +657,17 @@ 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 no-op context manager ``does_not_raise`` to serve -as a complement to ``raises``. For example: +It may be helpful to use ``nullcontext`` as a complement to ``raises``. + +For example: .. code-block:: python - from contextlib import contextmanager + from contextlib import nullcontext as does_not_raise + import pytest - @contextmanager - def does_not_raise(): - yield - - @pytest.mark.parametrize( "example_input,expectation", [ @@ -687,22 +684,3 @@ as a complement to ``raises``. For example: In the example above, the first three test cases should run unexceptionally, while the fourth should raise ``ZeroDivisionError``. - -If you're only supporting Python 3.7+, you can simply use ``nullcontext`` -to define ``does_not_raise``: - -.. code-block:: python - - from contextlib import nullcontext as does_not_raise - -Or, if you're supporting Python 3.3+ you can use: - -.. code-block:: python - - from contextlib import ExitStack as does_not_raise - -Or, if desired, you can ``pip install contextlib2`` and use: - -.. code-block:: python - - from contextlib2 import nullcontext as does_not_raise diff --git a/testing/python/raises.py b/testing/python/raises.py index 2d62e9109..45e22870e 100644 --- a/testing/python/raises.py +++ b/testing/python/raises.py @@ -82,13 +82,9 @@ class TestRaises: def test_does_not_raise(self, pytester: Pytester) -> None: pytester.makepyfile( """ - from contextlib import contextmanager + from contextlib import nullcontext as does_not_raise import pytest - @contextmanager - def does_not_raise(): - yield - @pytest.mark.parametrize('example_input,expectation', [ (3, does_not_raise()), (2, does_not_raise()), @@ -107,13 +103,9 @@ class TestRaises: def test_does_not_raise_does_raise(self, pytester: Pytester) -> None: pytester.makepyfile( """ - from contextlib import contextmanager + from contextlib import nullcontext as does_not_raise import pytest - @contextmanager - def does_not_raise(): - yield - @pytest.mark.parametrize('example_input,expectation', [ (0, does_not_raise()), (1, pytest.raises(ZeroDivisionError)),