[Zope3-checkins] SVN: zope.testing/branches/stub-testSetUp_in_layer/src/zope/testing/testrunner Implement layer.testSetUp and layer.testTearDown support

Stuart Bishop cvs-admin at zope.org
Fri Jun 16 08:02:16 EDT 2006


Log message for revision 68689:
  Implement layer.testSetUp and layer.testTearDown support

Changed:
  U   zope.testing/branches/stub-testSetUp_in_layer/src/zope/testing/testrunner-layers-api.txt
  U   zope.testing/branches/stub-testSetUp_in_layer/src/zope/testing/testrunner.py

-=-
Modified: zope.testing/branches/stub-testSetUp_in_layer/src/zope/testing/testrunner-layers-api.txt
===================================================================
--- zope.testing/branches/stub-testSetUp_in_layer/src/zope/testing/testrunner-layers-api.txt	2006-06-16 08:00:26 UTC (rev 68688)
+++ zope.testing/branches/stub-testSetUp_in_layer/src/zope/testing/testrunner-layers-api.txt	2006-06-16 12:02:13 UTC (rev 68689)
@@ -12,41 +12,44 @@
 Layers are generally implemented as classes using class methods.
 
 >>> class BaseLayer:
-...     @classmethod
 ...     def setUp(cls):
 ...         log('BaseLayer.setUp')
+...     setUp = classmethod(setUp)
 ...
-...     @classmethod
 ...     def tearDown(cls):
 ...         log('BaseLayer.tearDown')
+...     tearDown = classmethod(tearDown)
 ...
-...     @classmethod
 ...     def testSetUp(cls):
 ...         log('BaseLayer.testSetUp')
+...     testSetUp = classmethod(testSetUp)
 ...
-...     @classmethod
 ...     def testTearDown(cls):
 ...         log('BaseLayer.testTearDown')
+...     testTearDown = classmethod(testTearDown)
+...
 
 Layers can extend other layers. Note that they do not explicitly
 invoke the setup and teardown methods of other layers - the test runner
 does this for us in order to minimize the number of invokations.
 
 >>> class TopLayer(BaseLayer):
-...     @classmethod
 ...     def setUp(cls):
 ...         log('TopLayer.setUp')
+...     setUp = classmethod(setUp)
 ...
-...     @classmethod
 ...     def tearDown(cls):
 ...         log('TopLayer.tearDown')
+...     tearDown = classmethod(tearDown)
 ...
-...     @classmethod
 ...     def testSetUp(cls):
 ...         log('TopLayer.testSetUp')
+...     testSetUp = classmethod(testSetUp)
 ...
 ...     def testTearDown(cls):
 ...         log('TopLayer.testTearDown')
+...     testTearDown = classmethod(testTearDown)
+...
 
 Tests or test suites specify what layer they need by storing a reference
 in the 'layer' attribute.
@@ -158,7 +161,7 @@
 
 >>> report()
 BaseLayer.setUp
-BaseLayer.testSetup
+BaseLayer.testSetUp
 TestSpecifyingBaseLayer.setUp
 TestSpecifyingBaseLayer.test1
 TestSpecifyingBaseLayer.tearDown

Modified: zope.testing/branches/stub-testSetUp_in_layer/src/zope/testing/testrunner.py
===================================================================
--- zope.testing/branches/stub-testSetUp_in_layer/src/zope/testing/testrunner.py	2006-06-16 08:00:26 UTC (rev 68688)
+++ zope.testing/branches/stub-testSetUp_in_layer/src/zope/testing/testrunner.py	2006-06-16 12:02:13 UTC (rev 68689)
@@ -490,7 +490,7 @@
 
         if options.verbose > 0 or options.progress:
             print '  Running:'
-        result = TestResult(options, tests)
+        result = TestResult(options, tests, layer_name=name)
 
         t = time.time()
 
@@ -683,9 +683,14 @@
 
     max_width = 80
 
-    def __init__(self, options, tests):
+    def __init__(self, options, tests, layer_name=None):
         unittest.TestResult.__init__(self)
         self.options = options
+        # Calculate our list of relevant layers we need to call testSetUp
+        # and testTearDown on.
+        self.layers = []
+        if layer_name != 'unit':
+            gather_layers(layer_from_name(layer_name), self.layers)
         if options.progress:
             count = 0
             for test in tests:
@@ -728,7 +733,28 @@
 
         return ' ' + s[:room]
 
+    def testSetUp(self):
+        """A layer may define a setup method to be called before each
+        individual test.
+        """
+        for layer in reversed(self.layers):
+            if hasattr(layer, 'testSetUp'):
+                layer.testSetUp()
+
+    def testTearDown(self):
+        """A layer may define a teardown method to be called after each
+           individual test.
+           
+           This is useful for clearing the state of global
+           resources or resetting external systems such as relational
+           databases or daemons.
+        """
+        for layer in self.layers:
+            if hasattr(layer, 'testTearDown'):
+                layer.testTearDown()
+
     def startTest(self, test):
+        self.testSetUp()
         unittest.TestResult.startTest(self, test)
         testsRun = self.testsRun - 1
         count = test.countTestCases()
@@ -817,6 +843,7 @@
             sys.stdout.write('\r' + (' ' * self.last_width) + '\r')
 
     def stopTest(self, test):
+        self.testTearDown()
         if self.options.progress:
             self.last_width = self.test_width
         elif self.options.verbose > 1:



More information about the Zope3-Checkins mailing list