changeset 20:c668a9386194

Remove dependency of distutils unittest command on ElementTree -- use {{{bitten.util.xmlio}}} instead.
author cmlenz
date Fri, 17 Jun 2005 11:45:16 +0000
parents 9db5f8eddb0d
children 07053ecfb124
files bitten/setuptools/testrunner.py bitten/util/xmlio.py
diffstat 2 files changed, 37 insertions(+), 29 deletions(-) [+]
line wrap: on
line diff
--- a/bitten/setuptools/testrunner.py
+++ b/bitten/setuptools/testrunner.py
@@ -24,8 +24,7 @@
 from distutils.core import Command
 from unittest import _TextTestResult, TextTestRunner
 
-from elementtree.ElementTree import Element, ElementTree, SubElement
-
+from bitten.util.xmlio import Element, SubElement
 
 class XMLTestResult(_TextTestResult):
 
@@ -56,7 +55,6 @@
 
     def run(self, test):
         result = TextTestRunner.run(self, test)
-
         if not self.xml_stream:
             return result
 
@@ -72,20 +70,15 @@
                 status = 'failure'
                 tb = [f[1] for f in result.failures if f[0] is testcase][0]
 
-            test_elem = SubElement(root, 'test', file=filename,
-                                   name=str(testcase), status=status,
-                                   duration=str(timetaken))
-
+            test_elem = SubElement(root, 'test', file=filename, name=testcase,
+                                   status=status, duration=timetaken)
             description = testcase.shortDescription()
             if description:
-                desc_elem = SubElement(test_elem, 'description')
-                desc_elem.test = description
-
+                desc_elem = SubElement(test_elem, 'description')[description]
             if tb:
-                tb_elem = SubElement(test_elem, 'traceback')
-                tb_elem.text = tb
+                tb_elem = SubElement(test_elem, 'traceback')[tb]
 
-        ElementTree(root).write(self.xml_stream)
+        root.write(self.xml_stream, newlines=True)
         return result
 
 
@@ -117,7 +110,6 @@
             trace = Trace(ignoredirs=[sys.prefix, sys.exec_prefix],
                           trace=False, count=True)
             trace.runfunc(self._run_tests)
-            # make a report, telling it where you want output
             results = trace.results()
             real_stdout = sys.stdout
             sys.stdout = open(self.coverage_results, 'w')
--- a/bitten/util/xmlio.py
+++ b/bitten/util/xmlio.py
@@ -18,6 +18,7 @@
 #
 # Author: Christopher Lenz <cmlenz@gmx.de>
 
+import os
 try:
     from cStringIO import StringIO
 except ImportError:
@@ -88,27 +89,29 @@
     def __str__(self):
         """Return a string representation of the XML element."""
         buf = StringIO()
-        buf.write('<')
-        buf.write(self.tagname)
+        self.write(buf)
+        return buf.getvalue()
+
+    def write(self, out, newlines=False):
+        """Serializes the element and writes the XML to the given output
+        stream.
+        """
+        out.write('<')
+        out.write(self.tagname)
         for name, value in self.attrs.items():
-            buf.write(' ')
-            buf.write(name)
-            buf.write('="')
-            buf.write(self._escape_attr(value))
-            buf.write('"')
+            out.write(' %s="%s"' % (name, self._escape_attr(value)))
         if self.children:
-            buf.write('>')
+            out.write('>')
             for child in self.children:
                 if isinstance(child, Element):
-                    buf.write(str(child))
+                    child.write(out, newlines=newlines)
                 else:
-                    buf.write(self._escape_text(child))
-            buf.write('</')
-            buf.write(self.tagname)
-            buf.write('>')
+                    out.write(self._escape_text(child))
+            out.write('</' + self.tagname + '>')
         else:
-            buf.write('/>')
-        return buf.getvalue()
+            out.write('/>')
+        if newlines:
+            out.write(os.linesep)
 
     def _escape_text(self, text):
         return str(text).replace('&', '&amp;').replace('<', '&lt;') \
@@ -118,6 +121,19 @@
         return self._escape_text(attr).replace('"', '&#34;')
 
 
+class SubElement(Element):
+
+    __slots__ = []
+
+    def __init__(self, parent, tagname, **attrs):
+        """Create an XML element using the specified tag name.
+        
+        All keyword arguments are handled as attributes of the element.
+        """
+        Element.__init__(self, tagname, **attrs)
+        parent.children.append(self)
+
+
 def parse_xml(text):
     from xml.dom import minidom
     if isinstance(text, (str, unicode)):
Copyright (C) 2012-2017 Edgewall Software