Use assert statements in tests

Make assert statements print pretty output, including source.

This makes assert x == y more usable, as an alternative to self.assertEqual(x, y)

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.

Configuration [pretty-assert]

always-on
Default:

False

Type:

boolean

Sample configuration

The default configuration is equivalent to including the following in a unittest.cfg file.

[pretty-assert]
always-on = False

Command-line options

--pretty-assert DEFAULT

Add pretty output for “assert” statements

Plugin class reference: PrettyAssert

class nose2.plugins.prettyassert.PrettyAssert(*args, **kwargs)[source]

Add pretty output for “assert” statements

static addAssertDetail(extraDetail, exc, trace)[source]

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.

attribute resolution

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.