[Zodb-checkins] CVS: Zope3/src/zope/testing - doctestunit.py:1.5

Jim Fulton jim at zope.com
Tue Sep 23 15:12:38 EDT 2003


Update of /cvs-repository/Zope3/src/zope/testing
In directory cvs.zope.org:/tmp/cvs-serv19733/src/zope/testing

Modified Files:
	doctestunit.py 
Log Message:
Modified doctest-based test cases to behave more like other unittest
test cases. Doctest-based test cases look like other test cases in
testrunner (test.py) output and can now be selected by name with
test.py.


=== Zope3/src/zope/testing/doctestunit.py 1.4 => 1.5 ===
--- Zope3/src/zope/testing/doctestunit.py:1.4	Sun Sep 21 13:34:29 2003
+++ Zope3/src/zope/testing/doctestunit.py	Tue Sep 23 15:12:38 2003
@@ -27,8 +27,62 @@
 import tempfile
 import unittest
 
-class DocTestTestFailure(Exception):
-    """A doctest test failed"""
+class DocTestTestCase(unittest.TestCase):
+    """A test case that wraps a test function.
+
+    This is useful for slipping pre-existing test functions into the
+    PyUnit framework. Optionally, set-up and tidy-up functions can be
+    supplied. As with TestCase, the tidy-up ('tearDown') function will
+    always be called if the set-up ('setUp') function ran successfully.
+    """
+
+    def __init__(self, tester, name, doc, filename, lineno,
+                 setUp=None, tearDown=None):
+        unittest.TestCase.__init__(self)
+        (self.__tester, self.__name, self.__doc,
+         self.__filename, self.__lineno,
+         self.__setUp, self.__tearDown
+         ) = tester, name, doc, filename, lineno, setUp, tearDown
+
+    def setUp(self):
+        if self.__setUp is not None:
+            self.__setUp()
+
+    def tearDown(self):
+        if self.__tearDown is not None:
+            self.__tearDown()
+
+    def runTest(self):
+        old = sys.stdout
+        new = StringIO()
+        try:
+            sys.stdout = new
+            failures, tries = self.__tester.runstring(self.__doc, self.__name)
+        finally:
+            sys.stdout = old
+
+        if failures:
+            mess = new.getvalue()
+            lname = '.'.join(self.__name.split('.')[-1:])
+            lineno = self.__lineno or "0 (don't know line no)"
+            raise self.failureException(
+                'Failed doctest test for %s\n'
+                '  File "%s", line %s, in %s\n\n%s'
+                % (self.__name, self.__filename, lineno, lname, new.getvalue())
+                )
+
+    def id(self):
+        return self.__name
+
+    def __repr__(self):
+        name = self.__name.split('.')
+        return "%s (%s)" % (name[-1], '.'.join(name[:-1]))
+
+    __str__ = __repr__
+
+    def shortDescription(self):
+        return "Doctest: " + self.__name
+
 
 def DocTestSuite(module=None,
                  setUp=lambda: None,
@@ -65,17 +119,10 @@
             elif filename.endswith(".pyo"):
                 filename = filename[:-1]
 
-        def testfunc(args=(tester, name, doc, filename, lineno)):
-            setUp()
-            try:
-                _test(*args)
-            finally:
-                tearDown()
-                
-        suite.addTest(unittest.FunctionTestCase(
-            testfunc,
-            description = "doctest of "+name
-            ))
+        suite.addTest(DocTestTestCase(
+            tester, name, doc, filename, lineno,
+            setUp, tearDown))
+                      
 
     return suite
 
@@ -90,26 +137,6 @@
         module = __import__(module, globals(), locals(), ["*"])
 
     return module
-
-def _test(tester, name, doc, filename, lineno):
-    old = sys.stdout
-    new = StringIO()
-    try:
-        sys.stdout = new
-        failures, tries = tester.runstring(doc, name)
-    finally:
-        sys.stdout = old
-
-    if failures:
-        mess = new.getvalue()
-        lname = '.'.join(name.split('.')[-1:])
-        lineno = lineno or "0 (don't know line no)"
-        raise DocTestTestFailure(
-            'Failed doctest test for %s\n'
-            '  File "%s", line %s, in %s\n\n%s'
-            % (name, filename, lineno, lname, new.getvalue())
-            )
-    
 
 def _doc(name, object, tests, prefix, filename='', lineno=''):
     doc = getattr(object, '__doc__', '')




More information about the Zodb-checkins mailing list