1. Ned Batchelder
  2. coverage.py
  3. Issues
Issue #21 resolved

Why not add a XML (cobertura format) output ?

Anonymous created an issue

Hi,

I found here : https://software.sandia.gov/svn/public/tpl/coverage/branches/3.0.dev (referenced by https://software.sandia.gov/trac/acro/wiki/Development/Resources#TestCoverageAnalysis) an extended version of coverage.py module which support Cobertura output. With this XML support, we could integrate Python coverage report analysis into tools like Hudson, Sonar...

Why not add it into the main trunk ;) ? (the license seems to still be in GPL...)

Comments (5)

  1. cjulien

    I test the xml output, and i was happy, my code seemed to be full covered.... but it wasn't. In the xml I found this :

    <line branch="false" hits="1" number="37"/>
    <line branch="false" number="38"/>
    

    the line 37 was effectively covered, the 38 was not... with a

    <line branch="false" hits="0" number="38"/>
    

    it will really show the uncovered lines :) Here is my proposition for correction :

    --- xmlreport_orig.py
    +++ xmlreport.py
    @@ -64,7 +65,8 @@
                         # recorded here.
                         if not line in missing:
                             l.setAttribute("hits", str(1))
    -
    +                    else:
    +                        l.setAttribute("hits", str(0))
                         # Q: can we get info about whether this statement
                         # is a branch?  If so, that data should be
                         # used here.                    
    

    -------------------------------------------------

    Another issue in the xml is the filename field :

    <class branch-rate="0.0" complexity="0.0" filename="test/__init__" line-rate="1.0" name="__init__">
    

    the filename shown here is the filename without extension....but this is not my filename, it is init.py !

    I try to correct this in this way :

    --- xmlreport_orig.py
    +++ xmlreport.py
    @@ -40,7 +40,8 @@
                 # Create the 'lines' and 'package' XML elements, which
                 # are populated later.  Note that a package == a directory.
                 dirname, fname = os.path.split(cu.name)
    +           _, ext = os.path.splitext(cu.filename)
                 dirname = dirname or '.'
                 package = packages.setdefault(
                     dirname, [ doc.createElement("package"), {}, 0, 0, 0, 0 ] )
    @@ -48,7 +49,7 @@
                 c.appendChild(lines)
                 className = fname.replace('.', '_')
                 c.setAttribute("name", className)
    -            c.setAttribute("filename", cu.name)
    +            c.setAttribute("filename", cu.name + ext)
                 c.setAttribute("complexity", "0.0")
     
                 try:
    

    Hope it will be usefull :)

  2. Tomi Kallio

    cjulien's proposed fix for the "filename" attribute does the trick, at least Hudson's Cobertura plugin is able to find the right set of source files after applying the change. Here's a diff against the latest xmlreport.py:

    diff -r 3e9229efc215 coverage/xmlreport.py
    --- a/coverage/xmlreport.py	Wed Oct 28 08:39:31 2009 -0400
    +++ b/coverage/xmlreport.py	Thu Oct 29 00:51:25 2009 +0200
    @@ -97,7 +97,8 @@
             xclass.appendChild(xlines)
             className = fname.replace('.', '_')
             xclass.setAttribute("name", className)
    -        xclass.setAttribute("filename", os.path.split(cu.filename)[1])
    +        ext = os.path.splitext(cu.filename)[1]
    +        xclass.setAttribute("filename", cu.name + ext)
             xclass.setAttribute("complexity", "0.0")
     
             # For each statement, create an XML 'line' element.
    

    What the Cobertura plugin wants is a relative path to a source file from the directory where coverage was run. In Python 2.6, the right path for the "filename" attribute can also be got from "os.path.relpath(cu.filename)".

  3. Log in to comment