Use assert statements in tests¶
Make assert statements print pretty output, including source.
assert x == y more usable, as an alternative to
This plugin implements
outcomeDetail() and checks for event.exc_info
If it finds that an AssertionError happened, it will inspect the traceback and
add additional detail to the error report.
Default: False Type: boolean
The default configuration is equivalent to including the following in a
[pretty-assert] always-on = False
Plugin class reference: PrettyAssert¶
Add pretty output for “assert” statements
addAssertDetail(extraDetail, exc, trace)¶
Add details to output regarding AssertionError and its context
extraDetail: a list of lines which will be joined with newlines and added to the output for this test failure – defined as part of the event format
exc: the AssertionError exception which was thrown
trace: a traceback object for the exception
assert statement inspection¶
The prettyassert plugin works by inspecting the stack frame which raised an AssertionError. Unlike pytest’s assertion rewriting code, it does not modify the built-in AssertionError.
As a result, it is somewhat limited in its capabilities – it can only report the bound values from that stack frame. That means that this type of statement works well:
x = f() y = g() assert x == y
but this type of statement does not:
assert f() == g()
It will still run, but the prettyassert will tell you that f and g are functions, not what they evaluated to. This is probably not what you want.
The assertion inspection will resolve attributes, so that expressions like this will work as well:
assert x.foo == 1
But note that the attribute x.foo will be resolved twice in this case, if the assertion fails. Once when the assertion is evaluated, and again when it is inspected.
As a result, properties with dynamic values may not behave as expected under prettyassert inspection.