Skip to content

Commit 77dcac8

Browse files
committed
Improving sphinx docs based on feedback
1 parent b29f40a commit 77dcac8

File tree

4 files changed

+32
-13
lines changed

4 files changed

+32
-13
lines changed

doc/en/example/parametrize.rst

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -560,11 +560,13 @@ As the result:
560560
- The test ``test_eval[basic_2+4]`` passed.
561561
- The test ``test_eval[basic_6*9]`` was expected to fail and did fail.
562562

563-
Parametrizing conditional raising with ``pytest.raises``
563+
.. _`parametrizing_conditional_raising`:
564+
565+
Parametrizing conditional raising
564566
--------------------------------------------------------------------
565567

566-
Use ``pytest.raises`` and ``pytest.does_not_raise`` together with the
567-
``parametrize`` decorator to write parametrized tests in which some
568+
Use :func:`pytest.raises` and :func:`pytest.does_not_raise` together with the
569+
:ref:`pytest.mark.parametrize ref` decorator to write parametrized tests in which some
568570
tests raise exceptions and others do not. For example::
569571

570572
import pytest
@@ -580,5 +582,5 @@ tests raise exceptions and others do not. For example::
580582
with expectation:
581583
assert (6 / example_input) is not None
582584

583-
In this example, the first three tests should run unexceptionally,
584-
while the fourth test should raise ``ZeroDivisionError``.
585+
In this example, the first three test cases should run unexceptionally,
586+
while the fourth should raise ``ZeroDivisionError``.

doc/en/reference.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ pytest.raises
6161
.. autofunction:: pytest.raises(expected_exception: Exception, [match], [message])
6262
:with: excinfo
6363

64+
pytest.does_not_raise
65+
~~~~~~~~~~~~~~~~~~~~~
66+
67+
.. autofunction:: pytest.does_not_raise()
68+
:with: excinfo
69+
6470
pytest.deprecated_call
6571
~~~~~~~~~~~~~~~~~~~~~~
6672

src/_pytest/python_api.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,14 @@ def raises(expected_exception, *args, **kwargs):
622622
...
623623
>>> assert exc_info.type is ValueError
624624
625+
**Using with** ``pytest.mark.parametrize``
626+
627+
When using :ref:`pytest.mark.parametrize ref`
628+
it is possible to parametrize tests such that
629+
some runs raise an exception and others do not.
630+
631+
See :ref:`parametrizing_conditional_raising` for an example.
632+
625633
**Legacy form**
626634
627635
It is possible to specify a callable by passing a to-be-called lambda::
@@ -734,13 +742,13 @@ def __exit__(self, *tp):
734742

735743
@contextmanager
736744
def does_not_raise():
737-
r"""
745+
r'''
738746
This context manager is a complement to ``pytest.raises()`` that does
739747
*not* catch any exceptions raised by the code block.
740748
741749
742-
This is essentially a no-op but is useful when
743-
conditionally parameterizing tests that may or may not
750+
This is essentially a *no-op* but is useful when
751+
conditionally parametrizing tests that may or may not
744752
raise an error. For example::
745753
746754
@pytest.mark.parametrize('example_input,expectation', [
@@ -750,9 +758,14 @@ def does_not_raise():
750758
(0, raises(ZeroDivisionError)),
751759
])
752760
def test_division(example_input, expectation):
753-
'''Test how much I know division.'''
754-
with expectation:
761+
"""Test how much I know division."""
762+
with expectation as excinfo:
755763
assert (6 / example_input) is not None
756-
"""
764+
765+
Note that `excinfo` will be *None* when using
766+
``does_not_raise``. In the example above, `execinfo`
767+
will be `None` for the first three runs and
768+
an :class:`ExceptionInfo` instance on last run.
769+
'''
757770

758771
yield

testing/python/raises.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ def test_does_not_raise(self, testdir):
9898
testdir.makepyfile(
9999
"""
100100
import pytest
101-
import _pytest._code
102101
103102
@pytest.mark.parametrize('example_input,expectation', [
104103
(3, pytest.does_not_raise()),
@@ -119,7 +118,6 @@ def test_does_not_raise_does_raise(self, testdir):
119118
testdir.makepyfile(
120119
"""
121120
import pytest
122-
import _pytest._code
123121
124122
@pytest.mark.parametrize('example_input,expectation', [
125123
(0, pytest.does_not_raise()),

0 commit comments

Comments
 (0)