Documenting raises/does_not_raise + parametrize
This commit is contained in:
parent
afe9fd5ffd
commit
c166b80a8c
|
@ -559,3 +559,26 @@ As the result:
|
|||
- The test ``test_eval[1+7-8]`` passed, but the name is autogenerated and confusing.
|
||||
- The test ``test_eval[basic_2+4]`` passed.
|
||||
- The test ``test_eval[basic_6*9]`` was expected to fail and did fail.
|
||||
|
||||
Parametrizing conditional raising with ``pytest.raises``
|
||||
--------------------------------------------------------------------
|
||||
|
||||
Use ``pytest.raises`` and ``pytest.does_not_raise`` together with the
|
||||
``parametrize`` decorator to write parametrized tests in which some
|
||||
tests raise exceptions and others do not. For example::
|
||||
|
||||
import pytest
|
||||
|
||||
@pytest.mark.parametrize('example_input,expectation', [
|
||||
(3, pytest.does_not_raise()),
|
||||
(2, pytest.does_not_raise()),
|
||||
(1, pytest.does_not_raise()),
|
||||
(0, pytest.raises(ZeroDivisionError)),
|
||||
])
|
||||
def test_division(example_input, expectation):
|
||||
"""Test how much I know division."""
|
||||
with expectation:
|
||||
assert (6 / example_input) is not None
|
||||
|
||||
In this example, the first three tests should run unexceptionally,
|
||||
while the fourth test should raise ``ZeroDivisionError``.
|
||||
|
|
Loading…
Reference in New Issue