Ignore:
Timestamp:
6 Mar 2011, 15:41:27 (14 years ago)
Author:
uli
Message:

Brush up the main waeup.sirp unit test layer. It now groks waeup.sirp
if necessary only and cleans up the environment afterwards
properly. Also display of warning messages (all the erraneous messages
about missing templates and the like) is disabled now when grokking
waeup.sirp via the finetuned layer.

File:
1 edited

Legend:

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

    r5780 r5796  
    22"""
    33import os.path
     4import warnings
    45import grok
    56import zope.component
     
    78from zope.app.testing.functional import ZCMLLayer
    89from zope.component import getGlobalSiteManager
     10from zope.testing.cleanup import cleanUp
    911
    1012ftesting_zcml = os.path.join(
     
    1214FunctionalLayer = ZCMLLayer(ftesting_zcml, __name__, 'FunctionalLayer',
    1315                            allow_teardown=True)
    14 
    15 class WAeUPSIRPUnitTestLayer(object):
    16     """A layer for doctests that groks `waeup.sirp`.
    17     """
    18     @classmethod
    19     def setUp(self):
    20         import grok
    21         grok.testing.grok('waeup.sirp')
    22 
    23     @classmethod
    24     def tearDown(self):
    25         pass
    26 
    2716
    2817def setUpZope(test=None):
     
    5746    Returns ``True`` if grokking was done, ``False`` else.
    5847
     48    .. The following samples should go into Sphinx docs directly....
     49   
    5950    Sample
    6051    ******
     52
     53    Usage with plain Python testrunners
     54    -----------------------------------
    6155   
    6256    Together with the :func:`setUpZope` and :func:`cleanUpZope`
    6357    functions we then can do unittests with all components registered
    64     like this::
     58    and the event dispatcher in place like this::
    6559
    6660      import unittest2 as unittest # Want Python 2.7 features
     
    10195    won't work out of the box. The benefit is the faster test
    10296    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   
    103113    """
    104114    gsm =  getGlobalSiteManager()
     
    112122    warnings.simplefilter('default') # reenable warnings
    113123    return True
     124
     125
     126class 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.