Module
zope.testing.renormalizing

Regular expression pattern normalizing output checker

The pattern-normalizing output checker extends the default output checker with an option to normalize expected an actual output.

You specify a sequence of patterns and replacements. The replacements are applied to the expected and actual outputs before calling the default outputs checker. Let's look at an example. In this example, we have some times and addresses:

>>> want = '''\ ... ... completed in 1.234 seconds. ... ... ... completed in 123.234 seconds. ... ... ... completed in .234 seconds. ... ... ... completed in 1.234 seconds. ... ... '''

>>> got = '''\ ... ... completed in 1.235 seconds. ... ... ... completed in 123.233 seconds. ... ... ... completed in .231 seconds. ... ... ... completed in 1.23 seconds. ... ... '''

We may wish to consider these two strings to match, even though they differ in actual addresses and times. The default output checker will consider them different:

>>> doctest.OutputChecker().check_output(want, got, 0) False

We'll use the RENormalizing to normalize both the wanted and gotten strings to ignore differences in times and addresses:

>>> import re >>> checker = RENormalizing([ ... (re.compile([0-9]*[.][0-9]* seconds), <SOME NUMBER OF> seconds), ... (re.compile(at 0x[0-9a-f]+), at <SOME ADDRESS>), ... ])

>>> checker.check_output(want, got, 0) True

Usual OutputChecker options work as expected:

>>> want_ellided = '''\ ... ... completed in 1.234 seconds. ... ... ... ... completed in 1.234 seconds. ... ... '''

>>> checker.check_output(want_ellided, got, 0) False

>>> checker.check_output(want_ellided, got, doctest.ELLIPSIS) True

When we get differencs, we output them with normalized text:

>>> source = '''\ ... >>> do_something() ... ... completed in 1.234 seconds. ... ... ... ... completed in 1.234 seconds. ... ... '''

>>> example = doctest.Example(source, want_ellided)

>>> print checker.output_difference(example, got, 0) Expected: > completed in seconds. ... > completed in seconds. Got: > completed in seconds. > completed in seconds. > completed in seconds. > completed in seconds.

>>> print checker.output_difference(example, got, ... doctest.REPORT_NDIFF) Differences (ndiff with -expected +actual): - > - completed in seconds. - ... > completed in seconds. + > + completed in seconds. + + > + completed in seconds. + + > + completed in seconds. +

If the wanted text is empty, however, we don't transform the actual output. This is usful when writing tests. We leave the expected output empty, run the test, and use the actual output as expected, after reviewing it.

>>> source = '''\ ... >>> do_something() ... '''

>>> example = doctest.Example(source, \n) >>> print checker.output_difference(example, got, 0) Expected: Got: completed in 1.235 seconds. completed in 123.233 seconds. completed in .231 seconds. completed in 1.23 seconds.