From bfabd26c901e8e54dd52d7ad4e02aa902dce783e Mon Sep 17 00:00:00 2001 From: Ned Batchelder Date: Sun, 13 Sep 2009 15:01:30 -0400 Subject: First part of wiring up xml reports. --- coverage/xml.py | 49 ++++++++++++++++--------------------------------- 1 file changed, 16 insertions(+), 33 deletions(-) (limited to 'coverage/xml.py') diff --git a/coverage/xml.py b/coverage/xml.py index acbd7124..55fdc973 100644 --- a/coverage/xml.py +++ b/coverage/xml.py @@ -1,17 +1,16 @@ -"""Cobertura reporting""" +"""Cobertura reporting for coverage.py""" -import os -import sys +import os, sys import xml.dom.minidom from coverage.report import Reporter -class CoberturaReporter(Reporter): - """A reporter for writing the summary report.""" +class XmlReporter(Reporter): + """A reporter for writing Cobertura-style XML coverage results.""" def __init__(self, coverage, ignore_errors=False): - super(CoberturaReporter, self).__init__(coverage, ignore_errors) + super(XmlReporter, self).__init__(coverage, ignore_errors) def report(self, morfs, omit_prefixes=None, outfile=None): """Generate a Cobertura report for `morfs`. @@ -21,15 +20,12 @@ class CoberturaReporter(Reporter): modules to omit from the report. """ - # - # Initial setup - # + # Initial setup. if not outfile: outfile = sys.stdout self.find_code_units(morfs, omit_prefixes) - # - # Create the DOM that will store the data - # + + # Create the DOM that will store the data. impl = xml.dom.minidom.getDOMImplementation() docType = impl.createDocumentType( "coverage", None, @@ -43,10 +39,8 @@ class CoberturaReporter(Reporter): errors = False for cu in self.code_units: - # # Create the 'lines' and 'package' XML elements, which # are populated later. Note that a package == a directory. - # (dir, fname) = os.path.split(cu.name) print "HERE",dir,":",fname if dir is '': @@ -65,24 +59,21 @@ class CoberturaReporter(Reporter): try: statements, _, missing, readable = self.coverage._analyze(cu) - # - # for each statement, create an XML 'line' element - # + + # For each statement, create an XML 'line' element. for line in statements: l = doc.createElement("line") l.setAttribute("number", str(line)) - # + # Q: can we get info about the number of times # a statement is executed? If so, that should be # recorded here. - # if not line in missing: l.setAttribute("hits", str(1)) - # + # Q: can we get info about whether this statement # is a branch? If so, that data should be # used here. - # l.setAttribute("branch", "false") lines.appendChild(l) class_lines = 1.0*len(statements) @@ -90,10 +81,8 @@ class CoberturaReporter(Reporter): class_branches = 0.0 class_branch_hits = 0.0 - # # Finalize the statistics that are collected # in the XML DOM. - # c.setAttribute("line-rate", str(class_hits / (class_lines or 1.0))) c.setAttribute( "branch-rate", str(class_branch_hits / (class_branches or 1.0)) ) package[1][className] = c @@ -110,15 +99,11 @@ class CoberturaReporter(Reporter): outfile.write(fmt_err % (cu.name, typ.__name__, msg)) errors=True - # - # Don't write the XML data if we've encountered errors - # + # Don't write the XML data if we've encountered errors. if errors: return - # - # Populate the XML DOM with the package info - # + # Populate the XML DOM with the package info. for packageName, packageData in packages.items(): package = packageData[0]; packageXml.appendChild(package) @@ -133,7 +118,5 @@ class CoberturaReporter(Reporter): package.setAttribute( "branch-rate", str(packageData[4] / (packageData[5] or 1.0) )) package.setAttribute("complexity", "0.0") - # - # Use the DOM to write the output file - # - outfile.write( doc.toprettyxml() ) + # Use the DOM to write the output file. + outfile.write(doc.toprettyxml()) -- cgit v1.2.1