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

Shawn Brown 03sjbrown at gmail.com
Sun Mar 18 17:09:15 EDT 2018

Hi Floris and thanks for the feedback.

Unfortunately, this does not solve my usecase. I'm trying to handle cases
where the following statement would pass:

    assert myfunc(failing_input) == False

But where this next statement would fail using my custom report:

    assert myfunc(failing_input)

Calling myfunc() needs to return True or False (or at least Truthy or
Falsy)--this is locked-in behavior.

The assert_myfunc() in my original post is a wrapper that does basically
the same thing as your example. Although I omitted the native assert in my
examples because it is only evaluated when it's already guaranteed to pass.

On Sat, Mar 17, 2018 at 5:04 PM, Floris Bruynooghe <flub at devork.be> wrote:

> Hi Shawn,
> Shawn Brown <03sjbrown at gmail.com> writes:
> >  I understand how to use pytest_assertrepr_compare() to return custom
> > assertion reports--but this interface requires a comparison operator. I'm
> > hoping to write a plugin that makes custom reports for statements like:
> >
> >     assert myfunc(myobj)
> >
> > Where myfunc() returns True or False. But without an operator (e.g.
> "=="),
> > I can't intercept the results to build my custom report.
> After refreshing my mind on the AST-rewriting code again -that's always
> tricky- I think you have been looking into the wrong direction.  I
> believe something as simple as this might do:
> def test_foo():
>     assert myfunc(42)
> def myfunc(*args):
>     __tracebackhide__
>     if not args:
>        pytest.fail('Hello there\nLook, a multi-line message')
>        raise AssertionError('multi\nline')  # alternative, never gets here
>     return True
> Does this solve your usecase?
> Cheers,
> Floris
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/pytest-dev/attachments/20180318/92c57168/attachment.html>

More information about the pytest-dev mailing list