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

Jim Fulton jim at zope.com
Mon Mar 15 15:41:39 EST 2004


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

Modified Files:
	doctestunit.py 
Log Message:
Added basic post-mortem-debugging support for doctests. Only
unexpected exceptions are handled. We still need to think about how to
handle debugging mismatched output.

I'm not very happy with the way this is done now. But it works.


=== Zope3/src/zope/testing/doctestunit.py 1.8 => 1.9 ===
--- Zope3/src/zope/testing/doctestunit.py:1.8	Tue Feb  3 16:53:33 2004
+++ Zope3/src/zope/testing/doctestunit.py	Mon Mar 15 15:41:39 2004
@@ -27,6 +27,7 @@
 import tempfile
 import unittest
 
+
 class DocTestTestCase(unittest.TestCase):
     """A test case that wraps a test function.
 
@@ -52,6 +53,10 @@
         if self.__tearDown is not None:
             self.__tearDown()
 
+    def setDebugModeOn(self):
+        self.__tester.optionflags |= (
+            doctest.RUN_DEBUGGER_ON_UNEXPECTED_EXCEPTION)
+
     def runTest(self):
         old = sys.stdout
         new = StringIO()
@@ -226,20 +231,27 @@
         ])
     return testsrc
 
-def debug(module, name, pm=False):
+def debug_src(src, pm=False):
     """Debug a single doctest test doc string
 
-    Provide the module (or dotted name of the module) containing the
-    test to be debugged and the name (within the module) of the object
-    with the doc string with tests to be debugged.
-
+    The string is provided directly
     """
-    module = _normalizeModule(module)
-    testsrc = testsource(module, name)
+    # XXX we rely on an internal doctest function:
+    examples = doctest._extract_examples(src)
+    src = '\n'.join([
+        "%s%s" % (source, _expect(expect))
+        for (source, expect, lineno) in examples
+        ])
+    debug_script(src, pm)
+
+def debug_script(src, pm=False, globs=None):
+    "Debug a test script"
     srcfilename = tempfile.mktemp("doctestdebug.py")
-    open(srcfilename, 'w').write(testsrc)
-    globs = {}
-    globs.update(module.__dict__)
+    open(srcfilename, 'w').write(src)
+    if globs:
+        globs = globs.copy()
+    else:
+        globs = {}
 
     try:
         if pm:
@@ -254,3 +266,15 @@
             pdb.run("execfile(%r)" % srcfilename, globs, globs)
     finally:
         os.remove(srcfilename)
+
+def debug(module, name, pm=False):
+    """Debug a single doctest test doc string
+
+    Provide the module (or dotted name of the module) containing the
+    test to be debugged and the name (within the module) of the object
+    with the doc string with tests to be debugged.
+
+    """
+    module = _normalizeModule(module)
+    testsrc = testsource(module, name)
+    debug_src(testsource, pm, module.__dict__)




More information about the Zope3-Checkins mailing list