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

Floris Bruynooghe flub at devork.be
Sat Mar 17 17:04:08 EDT 2018


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    


More information about the pytest-dev mailing list