source: main/waeup.sirp/trunk/src/waeup/sirp/testing.py @ 6298

Last change on this file since 6298 was 6238, checked in by uli, 14 years ago

Add some functional doctest helpers.

  • Property svn:eol-style set to native
File size: 6.7 KB
Line 
1"""Testing support for :mod:`waeup.sirp`.
2"""
3import grok
4import doctest
5import os.path
6import re
7import warnings
8import zope.component
9import waeup.sirp
10from zope.app.testing.functional import (
11    ZCMLLayer, FunctionalTestSetup, getRootFolder, sync)
12from zope.component import getGlobalSiteManager
13from zope.security.testing import addCheckerPublic
14from zope.testing import renormalizing
15from zope.testing.cleanup import cleanUp
16
17ftesting_zcml = os.path.join(
18    os.path.dirname(waeup.sirp.__file__), 'ftesting.zcml')
19FunctionalLayer = ZCMLLayer(ftesting_zcml, __name__, 'FunctionalLayer',
20                            allow_teardown=True)
21
22def setUpZope(test=None):
23    """Initialize a Zope-compatible environment.
24
25    Currently, we only initialize the event machinery.
26    """
27    zope.component.eventtesting.setUp(test)
28
29def cleanUpZope(test=None):
30    """Clean up Zope-related registrations.
31
32    Cleans up all registrations and the like.
33    """
34    cleanUp()
35
36def maybe_grok():
37    """Try to grok the :mod:`waeup.sirp` package.
38
39    For many tests, even simple ones, we want the components defined
40    somewhere in the :mod:`waeup.sirp` package being registered. While
41    grokking the complete package can become expensive when done many
42    times, we only want to grok if it did not happen
43    before. Furthermore regrokking the whole package makes no sense if
44    done already.
45
46    :func:`maybe_grok` checks whether any eventhandlers are already
47    registered and does nothing in that case.
48
49    The grokking of :mod:`waeup.sirp` is done with warnings disabled.
50
51    Returns ``True`` if grokking was done, ``False`` else.
52
53    .. The following samples should go into Sphinx docs directly....
54
55    Sample
56    ******
57
58    Usage with plain Python testrunners
59    -----------------------------------
60
61    Together with the :func:`setUpZope` and :func:`cleanUpZope`
62    functions we then can do unittests with all components registered
63    and the event dispatcher in place like this::
64
65      import unittest2 as unittest # Want Python 2.7 features
66      from waeup.sirp.testing import (
67        maybe_grok, setUpZope, cleanUpZope,
68        )
69      from waeup.sirp.app import University
70
71      class MyTestCase(unittest.TestCase):
72
73          @classmethod
74          def setUpClass(cls):
75              grokked = maybe_grok()
76              if grokked:
77                  setUpZope(None)
78              return
79
80          @classmethod
81          def tearDownClass(cls):
82              cleanUpZope(None)
83
84          def setUp(self):
85              pass
86
87          def tearDown(self):
88              pass
89
90          def test_jambdata_in_site(self):
91              u = University()
92              self.assertTrue('jambdata' in u.keys())
93              return
94
95    Here the component registration is done only once for the whole
96    :class:`unittest.TestCase` and no ZODB is needed. That means
97    inside the tests you can expect to have all :mod:`waeup.sirp`
98    components (utilities, adapter, events) registered but as objects
99    have here still have no place inside a ZODB things like 'browsing'
100    won't work out of the box. The benefit is the faster test
101    setup/teardown.
102
103    .. note:: This works only with the default Python testrunners.
104
105         If you use the Zope testrunner (from :mod:`zope.testing`)
106         then you have to use appropriate layers like the
107         :class:`waeup.sirp.testing.WAeUPSIRPUnitTestLayer`.
108
109    Usage with :mod:`zope.testing` testrunners
110    ------------------------------------------
111
112    If you use the standard Zope testrunner, classmethods like
113    `setUpClass` are not executed. Instead you have to use a layer
114    like the one defined in this module.
115
116    .. seealso:: :class:`waeup.sirp.testing.WAeUPSIRPUnitTestLayer`
117
118    """
119    gsm =  getGlobalSiteManager()
120    # If there are any event handlers registered already, we assume
121    # that waeup.sirp was grokked already. There might be a batter
122    # check, though.
123    if len(list(gsm.registeredHandlers())) > 0:
124        return False
125    # Register the zope.Public permission, normally done via ZCML setup.
126    addCheckerPublic()
127    warnings.simplefilter('ignore') # disable (erraneous) warnings
128    grok.testing.grok('waeup.sirp')
129    warnings.simplefilter('default') # reenable warnings
130    return True
131
132
133class WAeUPSIRPUnitTestLayer(object):
134    """A layer for tests that groks `waeup.sirp`.
135
136    A Zope test layer that registers all :mod:`waeup.sirp` components
137    before attached tests are run and cleans this registrations up
138    afterwards. Also basic (non-waeup.sirp) components like the event
139    dispatcher machinery are registered, set up and cleaned up.
140
141    This layer does not provide a complete ZODB setup (and is
142    therefore much faster than complete functional setups) but does
143    only the registrations (which also takes some time, so running
144    this layer is slower than test cases that need none or only a
145    few registrations).
146
147    The registrations are done for all tests the layer is attached to
148    once before all these tests are run (and torn down once
149    afterwards).
150
151    To make use of this layer, you have to write a
152    :mod:`unittest.TestCase` class that provides an attribute called
153    ``layer`` with this class as value like this::
154
155      import unittest
156      from waeup.sirp.testing import WAeUPSIRPUnitTestLayer
157
158      class MyTestCase(unittest.TestCase):
159
160          layer = WAeUPSIRPUnitTestLayer
161
162          # per-test setups and real tests go here...
163          def test_foo(self):
164              self.assertEqual(1, 1)
165              return
166
167    """
168    @classmethod
169    def setUp(cls):
170        #setUpZope(None)
171        grokked = maybe_grok()
172        if grokked:
173            pass
174            #setUpZope(None)
175        return
176
177    @classmethod
178    def tearDown(cls):
179        cleanUpZope(None)
180
181checker = renormalizing.RENormalizing([
182    # Relevant normalizers from zope.testing.testrunner.tests:
183    (re.compile(r'\d+[.]\d\d\d seconds'), 'N.NNN seconds'),
184    # Our own one to work around
185    # http://reinout.vanrees.org/weblog/2009/07/16/invisible-test-diff.html:
186    (re.compile(r'.*1034h'), ''),
187    (re.compile(r'httperror_seek_wrapper:'), 'HTTPError:' )
188    ])
189
190def setUp(test):
191    FunctionalTestSetup().setUp()
192
193def tearDown(test):
194    FunctionalTestSetup().tearDown()
195
196def doctestsuite_for_module(dotted_path):
197    """Create a doctest suite for the module at `dotted_path`.
198    """
199    test = doctest.DocTestSuite(
200        dotted_path,
201        setUp = setUp,
202        tearDown = tearDown,
203        checker = checker,
204        extraglobs = dict(
205            getRootFolder=getRootFolder,
206            sync=sync,),
207        optionflags = (doctest.ELLIPSIS +
208                       doctest.NORMALIZE_WHITESPACE +
209                       doctest.REPORT_NDIFF),
210        )
211    test.layer = FunctionalLayer
212    return test
Note: See TracBrowser for help on using the repository browser.