[Zope-Checkins] SVN: Zope/branches/2.10/lib/python/Testing/ZopeTestCase/ Use a test_class to gain better control over fixture cleanup.

Stefan H. Holek stefan at epy.co.at
Sat Jun 23 11:02:53 EDT 2007


Log message for revision 76987:
  Use a test_class to gain better control over fixture cleanup.
  

Changed:
  U   Zope/branches/2.10/lib/python/Testing/ZopeTestCase/ZopeLite.py
  U   Zope/branches/2.10/lib/python/Testing/ZopeTestCase/zopedoctest/testPackageAsProduct.py

-=-
Modified: Zope/branches/2.10/lib/python/Testing/ZopeTestCase/ZopeLite.py
===================================================================
--- Zope/branches/2.10/lib/python/Testing/ZopeTestCase/ZopeLite.py	2007-06-23 13:18:57 UTC (rev 76986)
+++ Zope/branches/2.10/lib/python/Testing/ZopeTestCase/ZopeLite.py	2007-06-23 15:02:50 UTC (rev 76987)
@@ -216,6 +216,7 @@
 configure = Zope2.configure
 def startup(): pass
 Zope = Zope2
+active = _patched
 
 # ZODB sandbox factory
 from ZODB.DemoStorage import DemoStorage

Modified: Zope/branches/2.10/lib/python/Testing/ZopeTestCase/zopedoctest/testPackageAsProduct.py
===================================================================
--- Zope/branches/2.10/lib/python/Testing/ZopeTestCase/zopedoctest/testPackageAsProduct.py	2007-06-23 13:18:57 UTC (rev 76986)
+++ Zope/branches/2.10/lib/python/Testing/ZopeTestCase/zopedoctest/testPackageAsProduct.py	2007-06-23 15:02:50 UTC (rev 76987)
@@ -20,9 +20,12 @@
     execfile(os.path.join(sys.path[0], 'framework.py'))
 
 from unittest import TestSuite
-from Testing.ZopeTestCase import FunctionalDocTestSuite
+from Testing import ZopeTestCase
+from Testing.ZopeTestCase import ZopeLite
+from Testing.ZopeTestCase import ZopeDocTestSuite
 from Products.Five import zcml
 from zope.testing import cleanup
+import Products
 
 
 def testInstallPackage():
@@ -31,15 +34,12 @@
 
       >>> from Testing import ZopeTestCase
       >>> from Products.Five import zcml
-      >>> import sys, Products
 
-    Rig sys.path so testpackage can be imported
+    Register testpackage
 
-      >>> saved = sys.path[:]
-      >>> sys.path.append(ZopeTestCase.__path__[0])
+      >>> ZopeTestCase.hasPackage('testpackage')
+      False
 
-    Register testpackage
-
       >>> config = '''
       ... <configure
       ...     xmlns:five="http://namespaces.zope.org/five">
@@ -48,19 +48,18 @@
       ...     initialize="testpackage.initialize"
       ...     />
       ... </configure>'''
+      >>> zcml.load_string(config)
 
+    The package is registered now
+
       >>> ZopeTestCase.hasPackage('testpackage')
-      False
-      >>> zcml.load_string(config)
-      >>> ZopeTestCase.hasPackage('testpackage')
       True
 
-    Not yet installed
+    But not yet installed
 
-      >>> app = ZopeTestCase.app()
+      >>> app = self._app()
       >>> 'testpackage' in app.Control_Panel.Products.objectIds()
       False
-      >>> ZopeTestCase.close(app)
 
     Install it
 
@@ -69,39 +68,54 @@
 
     Now it shows up in Control_Panel
 
-      >>> app = ZopeTestCase.app()
+      >>> app = self._app()
       >>> 'testpackage' in app.Control_Panel.Products.objectIds()
       True
-      >>> ZopeTestCase.close(app)
 
     hasPackage still returns True
 
       >>> ZopeTestCase.hasPackage('testpackage')
       True
 
-    Clean up
+    A package is only installed once, subsequent calls to installPackage
+    are ignored:
 
-      >>> import testpackage
-      >>> Products._registered_packages.remove(testpackage)
-      >>> sys.path[:] = saved
+      >>> ZopeTestCase.installPackage('testpackage', quiet=True)
     """
 
 
-def setUp(self):
-    cleanup.cleanUp()
-    zcml._initialized = False
-    zcml.load_site()
+class TestClass(ZopeTestCase.FunctionalTestCase):
 
-def tearDown(self):
-    cleanup.cleanUp()
-    zcml._initialized = False
+    def afterSetUp(self):
+        cleanup.cleanUp()
+        zcml._initialized = False
+        zcml.load_site()
 
+        self.saved = sys.path[:]
+        sys.path.append(ZopeTestCase.__path__[0])
 
+    def afterClear(self):
+        cleanup.cleanUp()
+        sys.path[:] = self.saved
+
+        registered = getattr(Products, '_registered_packages', None)
+        if registered is not None:
+            Products._registered_packages = [m for m in registered
+                                             if m.__name__ != 'testpackage']
+
+        to_initialize = getattr(Products, '_packages_to_initialize', None)
+        if to_initialize is not None:
+            Products._packages_to_initialize = [(m, f) for (m, f) in to_initialize
+                                                if m.__name__ != 'testpackage']
+
+
 def test_suite():
-    return TestSuite((
-        # Must use functional because installPackage commits
-        FunctionalDocTestSuite(setUp=setUp, tearDown=tearDown),
-    ))
+    if ZopeLite.active:
+        return TestSuite((
+            ZopeDocTestSuite(test_class=TestClass),
+        ))
+    else:
+        return TestSuite()
 
 if __name__ == '__main__':
     framework()



More information about the Zope-Checkins mailing list