[pytest-dev] Custom reporting for asserts without comparison operators?

Floris Bruynooghe flub at devork.be
Thu Mar 22 17:46:10 EDT 2018

On Thu, Mar 22 2018, Shawn Brown wrote:

> Would a return value helper--as you are thinking about it--be able to
> handle cases like test_3passing()?
>     def test_3passing():
>         with pytest.raises(AssertionError) as excinfo:
>             assert myfunc(41)
>         assert 'custom report' in str(excinfo.value)

I agree with Ronny here and do think you're trying to design a very
weird and unnatural API for Python.  The function should either return
an object or raise an exception, you can't have both.  Well, you can as
you've show, but this is very brittle and your users will be thoroughly
confused about what is going on.  So you shouldn't have both.

Having followed this discussion so far I still think the most sane
approach is to let pytest show the repr of failing objects [0].  This would
allow you to write objects with a .__bool__() and a .__repr__() where
pytest would show the repr if it's false.  And if you really want to
have the dual behaviour, make it explicit to the user with a signature
like myfunc(value, raising=False) so they can invoke the raising
behaviour when desired.

[0] I don't think it currently does this, so this would be a feature
    request AFAIK.

More information about the pytest-dev mailing list