Merge branch 'main' into Feature-consider-nonparametrized-tests-in-reordering

This commit is contained in:
Sadra Barikbin 2023-08-05 13:57:34 +03:30 committed by GitHub
commit 76800a5c1c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 13 additions and 9 deletions

View File

@ -657,13 +657,16 @@ 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 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`` statements
target (``e`` in the example below).
For example: For example:
.. code-block:: python .. code-block:: python
from contextlib import nullcontext as does_not_raise from contextlib import nullcontext
import pytest import pytest
@ -671,16 +674,17 @@ For example:
@pytest.mark.parametrize( @pytest.mark.parametrize(
"example_input,expectation", "example_input,expectation",
[ [
(3, does_not_raise()), (3, nullcontext(2)),
(2, does_not_raise()), (2, nullcontext(3)),
(1, does_not_raise()), (1, nullcontext(6)),
(0, pytest.raises(ZeroDivisionError)), (0, pytest.raises(ZeroDivisionError)),
], ],
) )
def test_division(example_input, expectation): def test_division(example_input, expectation):
"""Test how much I know division.""" """Test how much I know division."""
with expectation: with expectation as e:
assert (6 / example_input) is not None assert (6 / example_input) == e
In the example above, the first three test cases should run unexceptionally, In the example above, the first three test cases should run without any
while the fourth should raise ``ZeroDivisionError``. exceptions, while the fourth should raise a``ZeroDivisionError`` exception,
which is expected by pytest.