Changeset 6463


Ignore:
Timestamp:
23 Jun 2011, 09:09:54 (13 years ago)
Author:
uli
Message:

Add doctest-like checking of ouput strings for regular unittests.

Extend/document output checkers for this kind of test.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.sirp/trunk/src/waeup/sirp/testing.py

    r6238 r6463  
    99import waeup.sirp
    1010from zope.app.testing.functional import (
    11     ZCMLLayer, FunctionalTestSetup, getRootFolder, sync)
     11    ZCMLLayer, FunctionalTestSetup, getRootFolder, sync, FunctionalTestCase)
    1212from zope.component import getGlobalSiteManager
    1313from zope.security.testing import addCheckerPublic
     
    179179        cleanUpZope(None)
    180180
     181#: This extended :class:`doctest.OutputChecker` allows the following
     182#: additional matches when looking for output diffs:
     183#:
     184#: `N.NNN seconds`
     185#:    matches strings like ``12.123 seconds``
     186#:
     187#: `HTTPError:`
     188#:    matches ``httperror_seek_wrapper:``. This string is output by some
     189#:    virtual browsers you might use in functional browser tests to signal
     190#:    HTTP error state.
     191#:
     192#: `1034h`
     193#:    is ignored. This sequence of control chars is output by some
     194#:    (buggy) testrunners at beginning of output.
     195#:
     196#: `<10-DIGITS>`
     197#:    matches a sequence of 10 digits. Useful when checking accesscode
     198#:    numbers if you don't know the exact (random) code.
     199#:
     200#: `<YYYY-MM-DD HH:MM:SS>`
     201#:    matches any date and time like `2011-05-01 12:01:32`.
     202#:
     203#: `<DATE-AND-TIME>`
     204#:    same like ``<YYYY-MM-DD HH:MM:SS>`` but shorter.
    181205checker = renormalizing.RENormalizing([
    182206    # Relevant normalizers from zope.testing.testrunner.tests:
     
    185209    # http://reinout.vanrees.org/weblog/2009/07/16/invisible-test-diff.html:
    186210    (re.compile(r'.*1034h'), ''),
    187     (re.compile(r'httperror_seek_wrapper:'), 'HTTPError:' )
     211    (re.compile(r'httperror_seek_wrapper:'), 'HTTPError:' ),
     212    (re.compile('[\d]{10}'), '<10-DIGITS>'),
     213    (re.compile('\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d'), '<YYYY-MM-DD HH:MM:SS>'),
     214    (re.compile('\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d'), '<DATE-AND-TIME>'),
    188215    ])
    189216
     
    211238    test.layer = FunctionalLayer
    212239    return test
     240
     241optionflags = (
     242    doctest.REPORT_NDIFF + doctest.ELLIPSIS + doctest.NORMALIZE_WHITESPACE)
     243
     244class FunctionalTestCase(FunctionalTestCase):
     245    """A test case that supports checking output diffs in doctest style.
     246    """
     247
     248    def assertMatches(self, want, got, checker=checker,
     249                      optionflags=optionflags):
     250        """Assert that the multiline string `want` matches `got`.
     251
     252        In `want` you can use shortcuts like ``...`` as in regular doctests.
     253
     254        If no special `checker` is passed, we use an extended
     255        :class:`doctest.OutputChecker` as defined in
     256        :mod:`waeup.sirp.testing`.
     257
     258        If optional `optionflags` are not given, use ``REPORT_NDIFF``,
     259        ``ELLIPSIS``, and ``NORMALIZE_WHITESPACE``.
     260
     261        .. seealso:: :data:`waeup.sirp.optionflags`
     262
     263        .. seealso:: :data:`waeup.sirp.checker`
     264        """
     265        if checker.check_output(want, got, optionflags):
     266            return
     267        diff = checker.output_difference(
     268            doctest.Example('', want), got, optionflags)
     269        self.fail(diff)
Note: See TracChangeset for help on using the changeset viewer.