-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Parameterize conditional raises #4679
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,7 @@ Anthony Shaw | |
Anthony Sottile | ||
Anton Lodder | ||
Antony Lee | ||
Arel Cordero | ||
Armin Rigo | ||
Aron Coyle | ||
Aron Curzon | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
A context manager ``does_not_raise`` is added to complement ``raises`` in parametrized tests with conditional raises. | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Document how to use ``raises`` and ``does_not_raise`` to write parametrized tests with conditional raises. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
import pprint | ||
import sys | ||
import warnings | ||
from contextlib import contextmanager | ||
from decimal import Decimal | ||
from numbers import Number | ||
|
||
|
@@ -621,6 +622,14 @@ def raises(expected_exception, *args, **kwargs): | |
... | ||
>>> assert exc_info.type is ValueError | ||
|
||
**Using with** ``pytest.mark.parametrize`` | ||
|
||
When using :ref:`pytest.mark.parametrize ref` | ||
it is possible to parametrize tests such that | ||
some runs raise an exception and others do not. | ||
|
||
See :ref:`parametrizing_conditional_raising` for an example. | ||
|
||
**Legacy form** | ||
|
||
It is possible to specify a callable by passing a to-be-called lambda:: | ||
|
@@ -726,3 +735,37 @@ def __exit__(self, *tp): | |
if self.match_expr is not None and suppress_exception: | ||
self.excinfo.match(self.match_expr) | ||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should have a warning here for the usage like
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks! I updated the sphinx documentation in 77dcac8 regarding |
||
|
||
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought we agreed that we were not going to add this? It encourages tests which are of bad form
#1830 (comment)
#4324 was a followup to #1830 that we document how to implement
does_not_raises
if you absolutely need it, not that we actually implement it.I was and still am 👎 on this.