Skip to content

Commit fd4289d

Browse files
committed
Adding does_not_raise to documentation only
1 parent 977adf1 commit fd4289d

File tree

6 files changed

+29
-54
lines changed

6 files changed

+29
-54
lines changed

changelog/1830.feature.rst

Lines changed: 0 additions & 1 deletion
This file was deleted.

doc/en/example/parametrize.rst

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -565,16 +565,25 @@ As the result:
565565
Parametrizing conditional raising
566566
--------------------------------------------------------------------
567567

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
570-
tests raise exceptions and others do not. For example::
568+
Use :func:`pytest.raises` with the
569+
:ref:`pytest.mark.parametrize ref` decorator to write parametrized tests
570+
in which some tests raise exceptions and others do not.
571571

572+
It is helpful to define a function such as ``does_not_raise`` to serve
573+
as a complement to ``raises``. For example::
574+
575+
from contextlib import contextmanager
572576
import pytest
573577

578+
@contextmanager
579+
def does_not_raise():
580+
yield
581+
582+
574583
@pytest.mark.parametrize('example_input,expectation', [
575-
(3, pytest.does_not_raise()),
576-
(2, pytest.does_not_raise()),
577-
(1, pytest.does_not_raise()),
584+
(3, does_not_raise()),
585+
(2, does_not_raise()),
586+
(1, does_not_raise()),
578587
(0, pytest.raises(ZeroDivisionError)),
579588
])
580589
def test_division(example_input, expectation):

doc/en/reference.rst

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,6 @@ 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-
7064
pytest.deprecated_call
7165
~~~~~~~~~~~~~~~~~~~~~~
7266

src/_pytest/python_api.py

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import pprint
55
import sys
66
import warnings
7-
from contextlib import contextmanager
87
from decimal import Decimal
98
from numbers import Number
109

@@ -735,37 +734,3 @@ def __exit__(self, *tp):
735734
if self.match_expr is not None and suppress_exception:
736735
self.excinfo.match(self.match_expr)
737736
return suppress_exception
738-
739-
740-
# builtin pytest.does_not_raise helper
741-
742-
743-
@contextmanager
744-
def does_not_raise():
745-
r'''
746-
This context manager is a complement to ``pytest.raises()`` that does
747-
*not* catch any exceptions raised by the code block.
748-
749-
750-
This is essentially a *no-op* but is useful when
751-
conditionally parametrizing tests that may or may not
752-
raise an error. For example::
753-
754-
@pytest.mark.parametrize('example_input,expectation', [
755-
(3, does_not_raise()),
756-
(2, does_not_raise()),
757-
(1, does_not_raise()),
758-
(0, raises(ZeroDivisionError)),
759-
])
760-
def test_division(example_input, expectation):
761-
"""Test how much I know division."""
762-
with expectation as excinfo:
763-
assert (6 / example_input) is not None
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-
'''
770-
771-
yield

src/pytest.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
from _pytest.python import Module
3333
from _pytest.python import Package
3434
from _pytest.python_api import approx
35-
from _pytest.python_api import does_not_raise
3635
from _pytest.python_api import raises
3736
from _pytest.recwarn import deprecated_call
3837
from _pytest.recwarn import warns
@@ -51,7 +50,6 @@
5150
"cmdline",
5251
"Collector",
5352
"deprecated_call",
54-
"does_not_raise",
5553
"exit",
5654
"fail",
5755
"File",

testing/python/raises.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,17 @@ def test_raise_wrong_exception_passes_by():
9797
def test_does_not_raise(self, testdir):
9898
testdir.makepyfile(
9999
"""
100+
from contextlib import contextmanager
100101
import pytest
101102
103+
@contextmanager
104+
def does_not_raise():
105+
yield
106+
102107
@pytest.mark.parametrize('example_input,expectation', [
103-
(3, pytest.does_not_raise()),
104-
(2, pytest.does_not_raise()),
105-
(1, pytest.does_not_raise()),
108+
(3, does_not_raise()),
109+
(2, does_not_raise()),
110+
(1, does_not_raise()),
106111
(0, pytest.raises(ZeroDivisionError)),
107112
])
108113
def test_division(example_input, expectation):
@@ -117,10 +122,15 @@ def test_division(example_input, expectation):
117122
def test_does_not_raise_does_raise(self, testdir):
118123
testdir.makepyfile(
119124
"""
125+
from contextlib import contextmanager
120126
import pytest
121127
128+
@contextmanager
129+
def does_not_raise():
130+
yield
131+
122132
@pytest.mark.parametrize('example_input,expectation', [
123-
(0, pytest.does_not_raise()),
133+
(0, does_not_raise()),
124134
(1, pytest.raises(ZeroDivisionError)),
125135
])
126136
def test_division(example_input, expectation):

0 commit comments

Comments
 (0)