Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why not add a XML (cobertura format) output ? #21

Closed
nedbat opened this issue Aug 27, 2009 · 5 comments
Closed

Why not add a XML (cobertura format) output ? #21

nedbat opened this issue Aug 27, 2009 · 5 comments
Labels
enhancement New feature or request

Comments

@nedbat
Copy link
Owner

nedbat commented Aug 27, 2009

Originally reported by Anonymous


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...)


@nedbat
Copy link
Owner Author

nedbat commented Aug 27, 2009

I have every intention of merging that work back into the coverage.py trunk. I just need to find a little time...

@nedbat
Copy link
Owner Author

nedbat commented Sep 14, 2009

This feature is now checked in (as of <<changeset df70d70a32b2 (bb)>>), ready for testing.

@nedbat
Copy link
Owner Author

nedbat commented Sep 22, 2009

Original comment by cjulien (Bitbucket: cjulien, GitHub: 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 :)

@nedbat
Copy link
Owner Author

nedbat commented Oct 28, 2009

Original comment by Tomi Kallio (Bitbucket: tomikall, GitHub: tomikall)


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 (bb)](https://bitbucket.org/ned/coveragepy/commits/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)".

@nedbat
Copy link
Owner Author

nedbat commented Nov 7, 2009

OK, I've applied this change (<<changeset e6f11f28504f (bb)>>). I suspect that cu.name is not always the right thing to use, but it certainly works better than before.

@nedbat nedbat closed this as completed Jun 29, 2012
agronholm added a commit to agronholm/coveragepy that referenced this issue Aug 16, 2020
This was necessary to implement receive_until() on TLS enabled connections.

Fixes nedbat#21.
@nedbat nedbat added enhancement New feature or request and removed proposal labels Oct 31, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant