Adding `does_not_raise` to documentation only

This commit is contained in:
Arel Cordero 2019-01-27 16:10:11 +00:00
parent 977adf1354
commit fd4289dae0
6 changed files with 29 additions and 54 deletions

View File

@ -1 +0,0 @@
A context manager ``does_not_raise`` is added to complement ``raises`` in parametrized tests with conditional raises.

View File

@ -565,16 +565,25 @@ As the result:
Parametrizing conditional raising Parametrizing conditional raising
-------------------------------------------------------------------- --------------------------------------------------------------------
Use :func:`pytest.raises` and :func:`pytest.does_not_raise` together with the Use :func:`pytest.raises` with the
:ref:`pytest.mark.parametrize ref` decorator to write parametrized tests in which some :ref:`pytest.mark.parametrize ref` decorator to write parametrized tests
tests raise exceptions and others do not. For example:: in which some tests raise exceptions and others do not.
It is helpful to define a function such as ``does_not_raise`` to serve
as a complement to ``raises``. For example::
from contextlib import contextmanager
import pytest import pytest
@contextmanager
def does_not_raise():
yield
@pytest.mark.parametrize('example_input,expectation', [ @pytest.mark.parametrize('example_input,expectation', [
(3, pytest.does_not_raise()), (3, does_not_raise()),
(2, pytest.does_not_raise()), (2, does_not_raise()),
(1, pytest.does_not_raise()), (1, does_not_raise()),
(0, pytest.raises(ZeroDivisionError)), (0, pytest.raises(ZeroDivisionError)),
]) ])
def test_division(example_input, expectation): def test_division(example_input, expectation):

View File

@ -61,12 +61,6 @@ pytest.raises
.. autofunction:: pytest.raises(expected_exception: Exception, [match], [message]) .. autofunction:: pytest.raises(expected_exception: Exception, [match], [message])
:with: excinfo :with: excinfo
pytest.does_not_raise
~~~~~~~~~~~~~~~~~~~~~
.. autofunction:: pytest.does_not_raise()
:with: excinfo
pytest.deprecated_call pytest.deprecated_call
~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~

View File

@ -4,7 +4,6 @@ import math
import pprint import pprint
import sys import sys
import warnings import warnings
from contextlib import contextmanager
from decimal import Decimal from decimal import Decimal
from numbers import Number from numbers import Number
@ -735,37 +734,3 @@ class RaisesContext(object):
if self.match_expr is not None and suppress_exception: if self.match_expr is not None and suppress_exception:
self.excinfo.match(self.match_expr) self.excinfo.match(self.match_expr)
return suppress_exception return suppress_exception
# builtin pytest.does_not_raise helper
@contextmanager
def does_not_raise():
r'''
This context manager is a complement to ``pytest.raises()`` that does
*not* catch any exceptions raised by the code block.
This is essentially a *no-op* but is useful when
conditionally parametrizing tests that may or may not
raise an error. For example::
@pytest.mark.parametrize('example_input,expectation', [
(3, does_not_raise()),
(2, does_not_raise()),
(1, does_not_raise()),
(0, raises(ZeroDivisionError)),
])
def test_division(example_input, expectation):
"""Test how much I know division."""
with expectation as excinfo:
assert (6 / example_input) is not None
Note that `excinfo` will be *None* when using
``does_not_raise``. In the example above, `execinfo`
will be `None` for the first three runs and
an :class:`ExceptionInfo` instance on last run.
'''
yield

View File

@ -32,7 +32,6 @@ from _pytest.python import Instance
from _pytest.python import Module from _pytest.python import Module
from _pytest.python import Package from _pytest.python import Package
from _pytest.python_api import approx from _pytest.python_api import approx
from _pytest.python_api import does_not_raise
from _pytest.python_api import raises from _pytest.python_api import raises
from _pytest.recwarn import deprecated_call from _pytest.recwarn import deprecated_call
from _pytest.recwarn import warns from _pytest.recwarn import warns
@ -51,7 +50,6 @@ __all__ = [
"cmdline", "cmdline",
"Collector", "Collector",
"deprecated_call", "deprecated_call",
"does_not_raise",
"exit", "exit",
"fail", "fail",
"File", "File",

View File

@ -97,12 +97,17 @@ class TestRaises(object):
def test_does_not_raise(self, testdir): def test_does_not_raise(self, testdir):
testdir.makepyfile( testdir.makepyfile(
""" """
from contextlib import contextmanager
import pytest import pytest
@contextmanager
def does_not_raise():
yield
@pytest.mark.parametrize('example_input,expectation', [ @pytest.mark.parametrize('example_input,expectation', [
(3, pytest.does_not_raise()), (3, does_not_raise()),
(2, pytest.does_not_raise()), (2, does_not_raise()),
(1, pytest.does_not_raise()), (1, does_not_raise()),
(0, pytest.raises(ZeroDivisionError)), (0, pytest.raises(ZeroDivisionError)),
]) ])
def test_division(example_input, expectation): def test_division(example_input, expectation):
@ -117,10 +122,15 @@ class TestRaises(object):
def test_does_not_raise_does_raise(self, testdir): def test_does_not_raise_does_raise(self, testdir):
testdir.makepyfile( testdir.makepyfile(
""" """
from contextlib import contextmanager
import pytest import pytest
@contextmanager
def does_not_raise():
yield
@pytest.mark.parametrize('example_input,expectation', [ @pytest.mark.parametrize('example_input,expectation', [
(0, pytest.does_not_raise()), (0, does_not_raise()),
(1, pytest.raises(ZeroDivisionError)), (1, pytest.raises(ZeroDivisionError)),
]) ])
def test_division(example_input, expectation): def test_division(example_input, expectation):