diff --git a/doc/en/example/parametrize.rst b/doc/en/example/parametrize.rst index 98aaeae3b..6c089fa41 100644 --- a/doc/en/example/parametrize.rst +++ b/doc/en/example/parametrize.rst @@ -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``.