[pytest-dev] [3.1 feature] "assert not raise exception" helper: opinions about the name

holger krekel holger at merlinux.eu
Thu Mar 30 15:32:39 EDT 2017

On Thu, Mar 30, 2017 at 16:12 +0000, Bruno Oliveira wrote:
> Hi all,
> We are about to land on the features branch an implementation of the
> proposal outlined by Florian Bruhin (
> https://github.com/pytest-dev/pytest/issues/1830) where we introduce a
> small helper to facilitate writing parametrized tests that sometimes raise
> an error for an input parameter and sometimes don’t:
> @pytest.mark.parametrize('inp, expectation', [
>     (-1, pytest.raises(ValueError)),
>     (3.5, pytest.raises(TypeError)),
>     (5, pytest.does_not_raise),
>     (10, pytest.does_not_raise),
> ])def test_bar(inp, expectation):
>     with expectation:
>         validate_positive_integer(inp)
> The pytest.does_not_raise helper actually does nothing, is meant solely to
> help writing this type of parametrized test.

It's a bit odd to introduce a new helper just for this particular case.
After skimming https://github.com/pytest-dev/pytest/issues/1830 
i'd prefer the mentioned pytest.raises(None) solution which lets through all exceptions
of the dependent code block.  Adding an example to the pytest docs and extending
the pytest.raises help string and implementation would be enough IMO.
By contrast, adding a new helper feels like unneccessary clutter of 
the pytest.* namespace. The above would then be:

 @pytest.mark.parametrize('inp, expectation', [
     (-1, ValueError),
     (3.5, TypeError),
     (5, None),
     (10, None)])
 def test_bar(inp, expectation):
     with pytest.raises(expectation):

where the parametrization is shorter and if one does not know
what pytest.raises(None) means one could find it easily in the
doc string or the pytest docs.


More information about the pytest-dev mailing list