update does_not_raise docs now that pytest is 3.7+ only

This commit is contained in:
Anthony Sottile 2022-06-29 13:13:15 -04:00
parent 06738e36df
commit 1b47de7d7f
2 changed files with 6 additions and 37 deletions

View File

@ -657,21 +657,17 @@ Use :func:`pytest.raises` with the
:ref:`pytest.mark.parametrize ref` decorator to write parametrized tests :ref:`pytest.mark.parametrize ref` decorator to write parametrized tests
in which some tests raise exceptions and others do not. 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 It may be helpful to use ``nullcontext`` as a complement to ``raises``.
as a complement to ``raises``. For example:
For example:
.. code-block:: python .. code-block:: python
from contextlib import contextmanager from contextlib import nullcontext as does_not_raise
import pytest import pytest
@contextmanager
def does_not_raise():
yield
@pytest.mark.parametrize( @pytest.mark.parametrize(
"example_input,expectation", "example_input,expectation",
[ [
@ -688,22 +684,3 @@ as a complement to ``raises``. For example:
In the example above, 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``. 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

View File

@ -92,13 +92,9 @@ class TestRaises:
def test_does_not_raise(self, pytester: Pytester) -> None: def test_does_not_raise(self, pytester: Pytester) -> None:
pytester.makepyfile( pytester.makepyfile(
""" """
from contextlib import contextmanager from contextlib import nullcontext as does_not_raise
import pytest import pytest
@contextmanager
def does_not_raise():
yield
@pytest.mark.parametrize('example_input,expectation', [ @pytest.mark.parametrize('example_input,expectation', [
(3, does_not_raise()), (3, does_not_raise()),
(2, does_not_raise()), (2, does_not_raise()),
@ -117,13 +113,9 @@ class TestRaises:
def test_does_not_raise_does_raise(self, pytester: Pytester) -> None: def test_does_not_raise_does_raise(self, pytester: Pytester) -> None:
pytester.makepyfile( pytester.makepyfile(
""" """
from contextlib import contextmanager from contextlib import nullcontext as does_not_raise
import pytest import pytest
@contextmanager
def does_not_raise():
yield
@pytest.mark.parametrize('example_input,expectation', [ @pytest.mark.parametrize('example_input,expectation', [
(0, does_not_raise()), (0, does_not_raise()),
(1, pytest.raises(ZeroDivisionError)), (1, pytest.raises(ZeroDivisionError)),