Changeset 5796 for main/waeup.sirp/trunk/src
- Timestamp:
- 6 Mar 2011, 15:41:27 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
main/waeup.sirp/trunk/src/waeup/sirp/testing.py
r5780 r5796 2 2 """ 3 3 import os.path 4 import warnings 4 5 import grok 5 6 import zope.component … … 7 8 from zope.app.testing.functional import ZCMLLayer 8 9 from zope.component import getGlobalSiteManager 10 from zope.testing.cleanup import cleanUp 9 11 10 12 ftesting_zcml = os.path.join( … … 12 14 FunctionalLayer = ZCMLLayer(ftesting_zcml, __name__, 'FunctionalLayer', 13 15 allow_teardown=True) 14 15 class WAeUPSIRPUnitTestLayer(object):16 """A layer for doctests that groks `waeup.sirp`.17 """18 @classmethod19 def setUp(self):20 import grok21 grok.testing.grok('waeup.sirp')22 23 @classmethod24 def tearDown(self):25 pass26 27 16 28 17 def setUpZope(test=None): … … 57 46 Returns ``True`` if grokking was done, ``False`` else. 58 47 48 .. The following samples should go into Sphinx docs directly.... 49 59 50 Sample 60 51 ****** 52 53 Usage with plain Python testrunners 54 ----------------------------------- 61 55 62 56 Together with the :func:`setUpZope` and :func:`cleanUpZope` 63 57 functions we then can do unittests with all components registered 64 like this::58 and the event dispatcher in place like this:: 65 59 66 60 import unittest2 as unittest # Want Python 2.7 features … … 101 95 won't work out of the box. The benefit is the faster test 102 96 setup/teardown. 97 98 .. note:: This works only with the default Python testrunners. 99 100 If you use the Zope testrunner (from :mod:`zope.testing`) 101 then you have to use appropriate layers like the 102 :class:`waeup.sirp.testing.WAeUPSIRPUnitTestLayer`. 103 104 Usage with :mod:`zope.testing` testrunners 105 ------------------------------------------ 106 107 If you use the standard Zope testrunner, classmethods like 108 `setUpClass` are not executed. Instead you have to use a layer 109 like the one defined in this module. 110 111 .. seealso:: :class:`waeup.sirp.testing.WAeUPSIRPUnitTestLayer` 112 103 113 """ 104 114 gsm = getGlobalSiteManager() … … 112 122 warnings.simplefilter('default') # reenable warnings 113 123 return True 124 125 126 class WAeUPSIRPUnitTestLayer(object): 127 """A layer for tests that groks `waeup.sirp`. 128 129 A Zope test layer that registers all :mod:`waeup.sirp` components 130 before attached tests are run and cleans this registrations up 131 afterwards. Also basic (non-waeup.sirp) components like the event 132 dispatcher machinery are registered, set up and cleaned up. 133 134 This layer does not provide a complete ZODB setup (and is 135 therefore much faster than complete functional setups) but does 136 only the registrations (which also takes some time, so running 137 this layer is slower than test cases that need none or only a 138 few registrations). 139 140 The registrations are done for all tests the layer is attached to 141 once before all these tests are run (and torn down once 142 afterwards). 143 144 To make use of this layer, you have to write a 145 :mod:`unittest.TestCase` class that provides an attribute called 146 ``layer`` with this class as value like this:: 147 148 import unittest 149 from waeup.sirp.testing import WAeUPSIRPUnitTestLayer 150 151 class MyTestCase(unittest.TestCase): 152 153 layer = WAeUPSIRPUnitTestLayer 154 155 # per-test setups and real tests go here... 156 def test_foo(self): 157 self.assertEqual(1, 1) 158 return 159 160 """ 161 @classmethod 162 def setUp(cls): 163 grokked = maybe_grok() 164 if grokked: 165 setUpZope(None) 166 return 167 168 @classmethod 169 def tearDown(cls): 170 cleanUpZope(None)
Note: See TracChangeset for help on using the changeset viewer.