Changeset 5780


Ignore:
Timestamp:
25 Feb 2011, 13:42:59 (14 years ago)
Author:
uli
Message:

Add more testing support. Add functions that help to setup a
reasonably configured environment during tests.

File:
1 edited

Legend:

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

    r5139 r5780  
     1"""Testing support for :mod:`waeup.sirp`.
     2"""
    13import os.path
     4import grok
     5import zope.component
    26import waeup.sirp
    37from zope.app.testing.functional import ZCMLLayer
     8from zope.component import getGlobalSiteManager
    49
    510ftesting_zcml = os.path.join(
     
    1924    def tearDown(self):
    2025        pass
     26
     27
     28def setUpZope(test=None):
     29    """Initialize a Zope-compatible environment.
     30
     31    Currently, we only initialize the event machinery.
     32    """
     33    zope.component.eventtesting.setUp(test)
     34
     35def cleanUpZope(test=None):
     36    """Clean up Zope-related registrations.
     37
     38    Cleans up all registrations and the like.
     39    """
     40    cleanUp()
     41
     42def maybe_grok():
     43    """Try to grok the :mod:`waeup.sirp` package.
     44
     45    For many tests, even simple ones, we want the components defined
     46    somewhere in the :mod:`waeup.sirp` package being registered. While
     47    grokking the complete package can become expensive when done many
     48    times, we only want to grok if it did not happen
     49    before. Furthermore regrokking the whole package makes no sense if
     50    done already.
     51
     52    :func:`maybe_grok` checks whether any eventhandlers are already
     53    registered and does nothing in that case.
     54
     55    The grokking of :mod:`waeup.sirp` is done with warnings disabled.
     56
     57    Returns ``True`` if grokking was done, ``False`` else.
     58
     59    Sample
     60    ******
     61   
     62    Together with the :func:`setUpZope` and :func:`cleanUpZope`
     63    functions we then can do unittests with all components registered
     64    like this::
     65
     66      import unittest2 as unittest # Want Python 2.7 features
     67      from waeup.sirp.testing import (
     68        maybe_grok, setUpZope, cleanUpZope,
     69        )
     70      from waeup.sirp.app import University
     71
     72      class MyTestCase(unittest.TestCase):
     73
     74          @classmethod
     75          def setUpClass(cls):
     76              grokked = maybe_grok()
     77              if grokked:
     78                  setUpZope(None)
     79              return
     80
     81          @classmethod
     82          def tearDownClass(cls):
     83              cleanUpZope(None)
     84
     85          def setUp(self):
     86              pass
     87       
     88          def tearDown(self):
     89              pass
     90
     91          def test_jambdata_in_site(self):
     92              u = University()
     93              self.assertTrue('jambdata' in u.keys())
     94              return
     95
     96    Here the component registration is done only once for the whole
     97    :class:`unittest.TestCase` and no ZODB is needed. That means
     98    inside the tests you can expect to have all :mod:`waeup.sirp`
     99    components (utilities, adapter, events) registered but as objects
     100    have here still have no place inside a ZODB things like 'browsing'
     101    won't work out of the box. The benefit is the faster test
     102    setup/teardown.
     103    """
     104    gsm =  getGlobalSiteManager()
     105    # If there are any event handlers registered already, we assume
     106    # that waeup.sirp was grokked already. There might be a batter
     107    # check, though.
     108    if len(list(gsm.registeredHandlers())) > 0:
     109        return False
     110    warnings.simplefilter('ignore') # disable (erraneous) warnings
     111    grok.testing.grok('waeup.sirp')
     112    warnings.simplefilter('default') # reenable warnings
     113    return True
Note: See TracChangeset for help on using the changeset viewer.