[7193] | 1 | ## $Id: testing.py 8186 2012-04-17 00:31:10Z uli $ |
---|
| 2 | ## |
---|
| 3 | ## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann |
---|
| 4 | ## This program is free software; you can redistribute it and/or modify |
---|
| 5 | ## it under the terms of the GNU General Public License as published by |
---|
| 6 | ## the Free Software Foundation; either version 2 of the License, or |
---|
| 7 | ## (at your option) any later version. |
---|
| 8 | ## |
---|
| 9 | ## This program is distributed in the hope that it will be useful, |
---|
| 10 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 11 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 12 | ## GNU General Public License for more details. |
---|
| 13 | ## |
---|
| 14 | ## You should have received a copy of the GNU General Public License |
---|
| 15 | ## along with this program; if not, write to the Free Software |
---|
| 16 | ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
| 17 | ## |
---|
[7811] | 18 | """Testing support for :mod:`waeup.kofa`. |
---|
[5780] | 19 | """ |
---|
[6238] | 20 | import grok |
---|
| 21 | import doctest |
---|
[6577] | 22 | import logging |
---|
[3521] | 23 | import os.path |
---|
[6238] | 24 | import re |
---|
[7581] | 25 | import tempfile |
---|
| 26 | import shutil |
---|
[6735] | 27 | import unittest |
---|
[5796] | 28 | import warnings |
---|
[5780] | 29 | import zope.component |
---|
[7811] | 30 | import waeup.kofa |
---|
[6238] | 31 | from zope.app.testing.functional import ( |
---|
[6463] | 32 | ZCMLLayer, FunctionalTestSetup, getRootFolder, sync, FunctionalTestCase) |
---|
[7581] | 33 | from zope.component import getGlobalSiteManager, queryUtility |
---|
[5865] | 34 | from zope.security.testing import addCheckerPublic |
---|
[6238] | 35 | from zope.testing import renormalizing |
---|
[5796] | 36 | from zope.testing.cleanup import cleanUp |
---|
[3521] | 37 | |
---|
| 38 | ftesting_zcml = os.path.join( |
---|
[7811] | 39 | os.path.dirname(waeup.kofa.__file__), 'ftesting.zcml') |
---|
[4789] | 40 | FunctionalLayer = ZCMLLayer(ftesting_zcml, __name__, 'FunctionalLayer', |
---|
| 41 | allow_teardown=True) |
---|
[5139] | 42 | |
---|
[6577] | 43 | def get_all_loggers(): |
---|
| 44 | """Get the keys of all logger defined globally. |
---|
| 45 | """ |
---|
[6727] | 46 | result = logging.root.manager.loggerDict.keys() |
---|
[7811] | 47 | ws_loggers = [x for x in result if 'waeup.kofa' in x] |
---|
[6747] | 48 | if ws_loggers: |
---|
[7817] | 49 | # For debugging: show any remaining loggers from w.k. namespace |
---|
[6747] | 50 | print "\nLOGGERS: ", ws_loggers |
---|
[6727] | 51 | return result |
---|
[6577] | 52 | |
---|
| 53 | def remove_new_loggers(old_loggers): |
---|
[6658] | 54 | """Remove the loggers except `old_loggers`. |
---|
[6577] | 55 | |
---|
| 56 | `old_loggers` is a list of logger keys as returned by |
---|
| 57 | :func:`get_all_loggers`. All globally registered loggers whose |
---|
| 58 | name is not in `old_loggers` is removed. |
---|
| 59 | """ |
---|
| 60 | new_loggers = [key for key in logging.root.manager.loggerDict |
---|
| 61 | if key not in old_loggers] |
---|
[7827] | 62 | for key in sorted(new_loggers, reverse=True): |
---|
[6578] | 63 | logger = logging.getLogger(key) |
---|
| 64 | for handler in logger.handlers: |
---|
| 65 | handler.close() |
---|
| 66 | del logger |
---|
[6577] | 67 | del logging.root.manager.loggerDict[key] |
---|
| 68 | return |
---|
| 69 | |
---|
[6578] | 70 | def remove_logger(name): |
---|
| 71 | """Remove logger with name `name`. |
---|
| 72 | |
---|
| 73 | Use is safe. If the logger does not exist nothing happens. |
---|
| 74 | """ |
---|
| 75 | if name in logging.root.manager.loggerDict.keys(): |
---|
| 76 | del logging.root.manager.loggerDict[name] |
---|
| 77 | return |
---|
| 78 | |
---|
| 79 | |
---|
[5780] | 80 | def setUpZope(test=None): |
---|
| 81 | """Initialize a Zope-compatible environment. |
---|
| 82 | |
---|
| 83 | Currently, we only initialize the event machinery. |
---|
| 84 | """ |
---|
| 85 | zope.component.eventtesting.setUp(test) |
---|
| 86 | |
---|
| 87 | def cleanUpZope(test=None): |
---|
| 88 | """Clean up Zope-related registrations. |
---|
| 89 | |
---|
| 90 | Cleans up all registrations and the like. |
---|
| 91 | """ |
---|
| 92 | cleanUp() |
---|
| 93 | |
---|
| 94 | def maybe_grok(): |
---|
[7811] | 95 | """Try to grok the :mod:`waeup.kofa` package. |
---|
[5780] | 96 | |
---|
| 97 | For many tests, even simple ones, we want the components defined |
---|
[7811] | 98 | somewhere in the :mod:`waeup.kofa` package being registered. While |
---|
[5780] | 99 | grokking the complete package can become expensive when done many |
---|
| 100 | times, we only want to grok if it did not happen |
---|
| 101 | before. Furthermore regrokking the whole package makes no sense if |
---|
| 102 | done already. |
---|
| 103 | |
---|
| 104 | :func:`maybe_grok` checks whether any eventhandlers are already |
---|
| 105 | registered and does nothing in that case. |
---|
| 106 | |
---|
[7811] | 107 | The grokking of :mod:`waeup.kofa` is done with warnings disabled. |
---|
[5780] | 108 | |
---|
| 109 | Returns ``True`` if grokking was done, ``False`` else. |
---|
| 110 | |
---|
[5796] | 111 | .. The following samples should go into Sphinx docs directly.... |
---|
[6237] | 112 | |
---|
[5780] | 113 | Sample |
---|
| 114 | ****** |
---|
[5796] | 115 | |
---|
| 116 | Usage with plain Python testrunners |
---|
| 117 | ----------------------------------- |
---|
[6237] | 118 | |
---|
[5780] | 119 | Together with the :func:`setUpZope` and :func:`cleanUpZope` |
---|
| 120 | functions we then can do unittests with all components registered |
---|
[5796] | 121 | and the event dispatcher in place like this:: |
---|
[5780] | 122 | |
---|
| 123 | import unittest2 as unittest # Want Python 2.7 features |
---|
[7811] | 124 | from waeup.kofa.testing import ( |
---|
[5780] | 125 | maybe_grok, setUpZope, cleanUpZope, |
---|
| 126 | ) |
---|
[7811] | 127 | from waeup.kofa.app import University |
---|
[5780] | 128 | |
---|
| 129 | class MyTestCase(unittest.TestCase): |
---|
| 130 | |
---|
| 131 | @classmethod |
---|
| 132 | def setUpClass(cls): |
---|
| 133 | grokked = maybe_grok() |
---|
| 134 | if grokked: |
---|
| 135 | setUpZope(None) |
---|
| 136 | return |
---|
| 137 | |
---|
| 138 | @classmethod |
---|
| 139 | def tearDownClass(cls): |
---|
| 140 | cleanUpZope(None) |
---|
| 141 | |
---|
| 142 | def setUp(self): |
---|
| 143 | pass |
---|
[6237] | 144 | |
---|
[5780] | 145 | def tearDown(self): |
---|
| 146 | pass |
---|
| 147 | |
---|
| 148 | def test_jambdata_in_site(self): |
---|
| 149 | u = University() |
---|
| 150 | self.assertTrue('jambdata' in u.keys()) |
---|
| 151 | return |
---|
| 152 | |
---|
| 153 | Here the component registration is done only once for the whole |
---|
| 154 | :class:`unittest.TestCase` and no ZODB is needed. That means |
---|
[7811] | 155 | inside the tests you can expect to have all :mod:`waeup.kofa` |
---|
[5780] | 156 | components (utilities, adapter, events) registered but as objects |
---|
| 157 | have here still have no place inside a ZODB things like 'browsing' |
---|
| 158 | won't work out of the box. The benefit is the faster test |
---|
| 159 | setup/teardown. |
---|
[5796] | 160 | |
---|
| 161 | .. note:: This works only with the default Python testrunners. |
---|
[6237] | 162 | |
---|
[5796] | 163 | If you use the Zope testrunner (from :mod:`zope.testing`) |
---|
| 164 | then you have to use appropriate layers like the |
---|
[7819] | 165 | :class:`waeup.kofa.testing.KofaUnitTestLayer`. |
---|
[5796] | 166 | |
---|
| 167 | Usage with :mod:`zope.testing` testrunners |
---|
| 168 | ------------------------------------------ |
---|
| 169 | |
---|
| 170 | If you use the standard Zope testrunner, classmethods like |
---|
| 171 | `setUpClass` are not executed. Instead you have to use a layer |
---|
| 172 | like the one defined in this module. |
---|
| 173 | |
---|
[7819] | 174 | .. seealso:: :class:`waeup.kofa.testing.KofaUnitTestLayer` |
---|
[6237] | 175 | |
---|
[5780] | 176 | """ |
---|
| 177 | gsm = getGlobalSiteManager() |
---|
| 178 | # If there are any event handlers registered already, we assume |
---|
[7811] | 179 | # that waeup.kofa was grokked already. There might be a batter |
---|
[5780] | 180 | # check, though. |
---|
| 181 | if len(list(gsm.registeredHandlers())) > 0: |
---|
| 182 | return False |
---|
[5865] | 183 | # Register the zope.Public permission, normally done via ZCML setup. |
---|
| 184 | addCheckerPublic() |
---|
[5780] | 185 | warnings.simplefilter('ignore') # disable (erraneous) warnings |
---|
[7811] | 186 | grok.testing.grok('waeup.kofa') |
---|
[5780] | 187 | warnings.simplefilter('default') # reenable warnings |
---|
| 188 | return True |
---|
[5796] | 189 | |
---|
[7581] | 190 | def setup_datacenter_conf(): |
---|
| 191 | """Register a datacenter config utility for non-functional tests. |
---|
| 192 | """ |
---|
[7811] | 193 | from waeup.kofa.interfaces import IDataCenterConfig |
---|
[7581] | 194 | conf = queryUtility(IDataCenterConfig) |
---|
| 195 | if conf is not None: |
---|
| 196 | return |
---|
| 197 | path = tempfile.mkdtemp() |
---|
| 198 | conf = {'path': path} |
---|
| 199 | gsm = getGlobalSiteManager() |
---|
| 200 | gsm.registerUtility(conf, IDataCenterConfig) |
---|
| 201 | return |
---|
[5796] | 202 | |
---|
[7581] | 203 | def teardown_datacenter_conf(): |
---|
| 204 | """Unregister a datacenter config utility for non-functional tests. |
---|
| 205 | """ |
---|
[7811] | 206 | from waeup.kofa.interfaces import IDataCenterConfig |
---|
[7581] | 207 | conf = queryUtility(IDataCenterConfig) |
---|
| 208 | if conf is None: |
---|
| 209 | return |
---|
| 210 | path = conf['path'] |
---|
| 211 | shutil.rmtree(path) |
---|
| 212 | gsm = getGlobalSiteManager() |
---|
| 213 | gsm.unregisterUtility(conf) |
---|
| 214 | return |
---|
| 215 | |
---|
[7819] | 216 | class KofaUnitTestLayer(object): |
---|
[7811] | 217 | """A layer for tests that groks `waeup.kofa`. |
---|
[5796] | 218 | |
---|
[7811] | 219 | A Zope test layer that registers all :mod:`waeup.kofa` components |
---|
[5796] | 220 | before attached tests are run and cleans this registrations up |
---|
[7811] | 221 | afterwards. Also basic (non-waeup.kofa) components like the event |
---|
[5796] | 222 | dispatcher machinery are registered, set up and cleaned up. |
---|
| 223 | |
---|
| 224 | This layer does not provide a complete ZODB setup (and is |
---|
| 225 | therefore much faster than complete functional setups) but does |
---|
| 226 | only the registrations (which also takes some time, so running |
---|
| 227 | this layer is slower than test cases that need none or only a |
---|
| 228 | few registrations). |
---|
| 229 | |
---|
| 230 | The registrations are done for all tests the layer is attached to |
---|
| 231 | once before all these tests are run (and torn down once |
---|
| 232 | afterwards). |
---|
[6237] | 233 | |
---|
[5796] | 234 | To make use of this layer, you have to write a |
---|
| 235 | :mod:`unittest.TestCase` class that provides an attribute called |
---|
| 236 | ``layer`` with this class as value like this:: |
---|
| 237 | |
---|
| 238 | import unittest |
---|
[7819] | 239 | from waeup.kofa.testing import KofaUnitTestLayer |
---|
[6237] | 240 | |
---|
[5796] | 241 | class MyTestCase(unittest.TestCase): |
---|
| 242 | |
---|
[7819] | 243 | layer = KofaUnitTestLayer |
---|
[5796] | 244 | |
---|
| 245 | # per-test setups and real tests go here... |
---|
| 246 | def test_foo(self): |
---|
| 247 | self.assertEqual(1, 1) |
---|
| 248 | return |
---|
| 249 | |
---|
| 250 | """ |
---|
| 251 | @classmethod |
---|
| 252 | def setUp(cls): |
---|
[5865] | 253 | #setUpZope(None) |
---|
[5796] | 254 | grokked = maybe_grok() |
---|
[7581] | 255 | setup_datacenter_conf() |
---|
[5796] | 256 | return |
---|
| 257 | |
---|
| 258 | @classmethod |
---|
| 259 | def tearDown(cls): |
---|
| 260 | cleanUpZope(None) |
---|
[7581] | 261 | teardown_datacenter_conf() |
---|
| 262 | return |
---|
[6238] | 263 | |
---|
[7581] | 264 | |
---|
[6463] | 265 | #: This extended :class:`doctest.OutputChecker` allows the following |
---|
| 266 | #: additional matches when looking for output diffs: |
---|
| 267 | #: |
---|
| 268 | #: `N.NNN seconds` |
---|
| 269 | #: matches strings like ``12.123 seconds`` |
---|
| 270 | #: |
---|
| 271 | #: `HTTPError:` |
---|
| 272 | #: matches ``httperror_seek_wrapper:``. This string is output by some |
---|
| 273 | #: virtual browsers you might use in functional browser tests to signal |
---|
| 274 | #: HTTP error state. |
---|
| 275 | #: |
---|
| 276 | #: `1034h` |
---|
| 277 | #: is ignored. This sequence of control chars is output by some |
---|
| 278 | #: (buggy) testrunners at beginning of output. |
---|
| 279 | #: |
---|
| 280 | #: `<10-DIGITS>` |
---|
| 281 | #: matches a sequence of 10 digits. Useful when checking accesscode |
---|
| 282 | #: numbers if you don't know the exact (random) code. |
---|
| 283 | #: |
---|
[6466] | 284 | #: `<YYYY-MM-DD hh:mm:ss>` |
---|
[6463] | 285 | #: matches any date and time like `2011-05-01 12:01:32`. |
---|
| 286 | #: |
---|
| 287 | #: `<DATE-AND-TIME>` |
---|
[6466] | 288 | #: same like ``<YYYY-MM-DD hh:mm:ss>`` but shorter. |
---|
[6238] | 289 | checker = renormalizing.RENormalizing([ |
---|
| 290 | # Relevant normalizers from zope.testing.testrunner.tests: |
---|
| 291 | (re.compile(r'\d+[.]\d\d\d seconds'), 'N.NNN seconds'), |
---|
| 292 | # Our own one to work around |
---|
| 293 | # http://reinout.vanrees.org/weblog/2009/07/16/invisible-test-diff.html: |
---|
| 294 | (re.compile(r'.*1034h'), ''), |
---|
[6463] | 295 | (re.compile(r'httperror_seek_wrapper:'), 'HTTPError:' ), |
---|
| 296 | (re.compile('[\d]{10}'), '<10-DIGITS>'), |
---|
[8186] | 297 | (re.compile('\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d [\+\-]\d\d\d\d [^ ]+'), |
---|
| 298 | '<YYYY-MM-DD hh:mm:ss TZ>'), |
---|
[6466] | 299 | (re.compile('\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d'), '<YYYY-MM-DD hh:mm:ss>'), |
---|
| 300 | (re.compile('\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d'), '<DATETIME>'), |
---|
[6238] | 301 | ]) |
---|
| 302 | |
---|
[6727] | 303 | old_loggers = [] |
---|
[6238] | 304 | def setUp(test): |
---|
[6727] | 305 | old_loggers = get_all_loggers() |
---|
[6238] | 306 | FunctionalTestSetup().setUp() |
---|
| 307 | |
---|
| 308 | def tearDown(test): |
---|
| 309 | FunctionalTestSetup().tearDown() |
---|
[6727] | 310 | remove_new_loggers(old_loggers) |
---|
[6238] | 311 | |
---|
| 312 | def doctestsuite_for_module(dotted_path): |
---|
| 313 | """Create a doctest suite for the module at `dotted_path`. |
---|
| 314 | """ |
---|
| 315 | test = doctest.DocTestSuite( |
---|
| 316 | dotted_path, |
---|
| 317 | setUp = setUp, |
---|
| 318 | tearDown = tearDown, |
---|
| 319 | checker = checker, |
---|
| 320 | extraglobs = dict( |
---|
| 321 | getRootFolder=getRootFolder, |
---|
| 322 | sync=sync,), |
---|
| 323 | optionflags = (doctest.ELLIPSIS + |
---|
| 324 | doctest.NORMALIZE_WHITESPACE + |
---|
| 325 | doctest.REPORT_NDIFF), |
---|
| 326 | ) |
---|
| 327 | test.layer = FunctionalLayer |
---|
| 328 | return test |
---|
[6463] | 329 | |
---|
| 330 | optionflags = ( |
---|
| 331 | doctest.REPORT_NDIFF + doctest.ELLIPSIS + doctest.NORMALIZE_WHITESPACE) |
---|
| 332 | |
---|
[6754] | 333 | def clear_logger_collector(): |
---|
| 334 | from zope.component import queryUtility, getGlobalSiteManager |
---|
[7811] | 335 | from waeup.kofa.interfaces import ILoggerCollector |
---|
[6754] | 336 | collector = queryUtility(ILoggerCollector) |
---|
| 337 | if collector is None: |
---|
| 338 | return |
---|
[6980] | 339 | keys = list(collector.keys()) |
---|
[6754] | 340 | for key in keys: |
---|
| 341 | del collector[key] |
---|
| 342 | return |
---|
| 343 | |
---|
[6463] | 344 | class FunctionalTestCase(FunctionalTestCase): |
---|
| 345 | """A test case that supports checking output diffs in doctest style. |
---|
| 346 | """ |
---|
| 347 | |
---|
[6578] | 348 | def setUp(self): |
---|
| 349 | super(FunctionalTestCase, self).setUp() |
---|
| 350 | self.functional_old_loggers = get_all_loggers() |
---|
| 351 | return |
---|
| 352 | |
---|
| 353 | def tearDown(self): |
---|
| 354 | super(FunctionalTestCase, self).tearDown() |
---|
| 355 | remove_new_loggers(self.functional_old_loggers) |
---|
[6754] | 356 | clear_logger_collector() |
---|
[6578] | 357 | return |
---|
| 358 | |
---|
[6463] | 359 | def assertMatches(self, want, got, checker=checker, |
---|
| 360 | optionflags=optionflags): |
---|
| 361 | """Assert that the multiline string `want` matches `got`. |
---|
| 362 | |
---|
| 363 | In `want` you can use shortcuts like ``...`` as in regular doctests. |
---|
| 364 | |
---|
| 365 | If no special `checker` is passed, we use an extended |
---|
| 366 | :class:`doctest.OutputChecker` as defined in |
---|
[7811] | 367 | :mod:`waeup.kofa.testing`. |
---|
[6463] | 368 | |
---|
| 369 | If optional `optionflags` are not given, use ``REPORT_NDIFF``, |
---|
| 370 | ``ELLIPSIS``, and ``NORMALIZE_WHITESPACE``. |
---|
| 371 | |
---|
[7811] | 372 | .. seealso:: :data:`waeup.kofa.testing.optionflags` |
---|
[6463] | 373 | |
---|
[7811] | 374 | .. seealso:: :data:`waeup.kofa.testing.checker` |
---|
[6463] | 375 | """ |
---|
| 376 | if checker.check_output(want, got, optionflags): |
---|
| 377 | return |
---|
| 378 | diff = checker.output_difference( |
---|
| 379 | doctest.Example('', want), got, optionflags) |
---|
| 380 | self.fail(diff) |
---|
[6727] | 381 | |
---|
| 382 | class FunctionalTestSetup(FunctionalTestSetup): |
---|
| 383 | """A replacement for the zope.app.testing class. |
---|
| 384 | |
---|
| 385 | Removes also loggers. |
---|
| 386 | """ |
---|
| 387 | |
---|
| 388 | def setUp(self): |
---|
| 389 | self.old_loggers = get_all_loggers() |
---|
| 390 | super(FunctionalTestSetup, self).setUp() |
---|
| 391 | return |
---|
| 392 | |
---|
| 393 | def tearDown(self): |
---|
| 394 | super(FunctionalTestSetup, self).tearDown() |
---|
| 395 | remove_new_loggers(self.old_loggers) |
---|
| 396 | return |
---|
[6735] | 397 | |
---|
| 398 | def get_doctest_suite(filename_list=[]): |
---|
| 399 | """Helper function to create doctest suites for doctests. |
---|
| 400 | |
---|
| 401 | The `filename_list` is a list of filenames relative to the |
---|
[7817] | 402 | w.k. dir. So, to get a doctest suite for ``browser.txt`` and |
---|
[6735] | 403 | ``blah.txt`` in the ``browser/`` subpackage you have to pass |
---|
| 404 | ``filename_list=['browser/browser.txt','browser/blah.txt']`` and |
---|
| 405 | so on. |
---|
| 406 | |
---|
| 407 | The returned test suite must be registered somewhere locally for |
---|
| 408 | instance by something like: |
---|
| 409 | |
---|
[7811] | 410 | from waeup.kofa.testing import get_doctest_suite |
---|
[6735] | 411 | def test_suite(): |
---|
| 412 | suite = get_doctest_suite(['mypkg/foo.txt', 'mypkg/bar.txt']) |
---|
| 413 | return suite |
---|
| 414 | |
---|
| 415 | and that's it. |
---|
| 416 | """ |
---|
| 417 | suite = unittest.TestSuite() |
---|
| 418 | for filename in filename_list: |
---|
| 419 | path = os.path.join( |
---|
| 420 | os.path.dirname(__file__), filename) |
---|
| 421 | test = doctest.DocFileSuite( |
---|
| 422 | path, |
---|
| 423 | module_relative=False, |
---|
| 424 | setUp=setUp, tearDown=tearDown, |
---|
| 425 | globs = dict(getRootFolder = getRootFolder), |
---|
| 426 | optionflags = doctest.ELLIPSIS + doctest.NORMALIZE_WHITESPACE, |
---|
| 427 | checker = checker, |
---|
| 428 | ) |
---|
| 429 | test.layer = FunctionalLayer |
---|
| 430 | suite.addTest(test) |
---|
| 431 | return suite |
---|