diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2009-11-26 21:20:48 -0500 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2009-11-26 21:20:48 -0500 |
commit | 72b1c5803d59deefb891bf1f5668b7a74160356b (patch) | |
tree | 22f8f98f37d1c3bf29d6d930a49d89cbc3793974 | |
parent | c079c0afa6f00bb89fe40e00508ac469f7b14cbb (diff) | |
download | python-coveragepy-git-72b1c5803d59deefb891bf1f5668b7a74160356b.tar.gz |
XML report now includes branch information.
-rw-r--r-- | CHANGES.txt | 4 | ||||
-rw-r--r-- | TODO.txt | 2 | ||||
-rw-r--r-- | coverage/xmlreport.py | 35 | ||||
-rw-r--r-- | test/farm/html/gold_y_xml_branch/coverage.xml | 22 | ||||
-rw-r--r-- | test/farm/html/run_y_xml_branch.py | 20 | ||||
-rw-r--r-- | test/farm/html/src/y.py | 9 |
6 files changed, 78 insertions, 14 deletions
diff --git a/CHANGES.txt b/CHANGES.txt index 4320c708..8f5e895a 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -5,6 +5,10 @@ Change history for Coverage.py Version 3.2b4
-------------
+- Branch coverage improvements:
+
+ - The XML report now includes branch information.
+
- Click-to-sort HTML report columns are now persisted in a cookie. Viewing
a report will sort it first the way you last had a coverage report sorted.
Thanks, `Chris Adams`_.
@@ -13,7 +13,7 @@ Coverage TODO (see top of test_cogapp for examples)
+ Maybe turning off yellow lines should make those lines green?
+ A missing branch to leave the function shows an annotation of -1. Now "exit".
-- XML report needs to get branch information.
++ XML report needs to get branch information.
- Add branch info to "coverage debug data"
diff --git a/coverage/xmlreport.py b/coverage/xmlreport.py index 2e31d9e9..f3a9b9c4 100644 --- a/coverage/xmlreport.py +++ b/coverage/xmlreport.py @@ -20,6 +20,7 @@ class XmlReporter(Reporter): self.packages = None self.xml_out = None + self.arcs = coverage.data.has_arcs() def report(self, morfs, omit_prefixes=None, outfile=None): """Generate a Cobertura-compatible XML report for `morfs`. @@ -102,27 +103,35 @@ class XmlReporter(Reporter): xclass.setAttribute("filename", cu.name + ext) xclass.setAttribute("complexity", "0.0") + branch_lines = analysis.branch_lines() + # For each statement, create an XML 'line' element. for line in analysis.statements: - l = self.xml_out.createElement("line") - l.setAttribute("number", str(line)) + xline = self.xml_out.createElement("line") + xline.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. - l.setAttribute("hits", str(int(not line in analysis.missing))) + # Q: can we get info about the number of times a statement is + # executed? If so, that should be recorded here. + xline.setAttribute("hits", str(int(not line in analysis.missing))) - # Q: can we get info about whether this statement - # is a branch? If so, that data should be - # used here. - #l.setAttribute("branch", "false") - xlines.appendChild(l) + if self.arcs: + if line in branch_lines: + xline.setAttribute("branch", "true") + xlines.appendChild(xline) class_lines = 1.0 * len(analysis.statements) class_hits = class_lines - len(analysis.missing) - class_branches = 0.0 - class_branch_hits = 0.0 + if self.arcs: + # We assume here that every branch line has 2 exits, which is usually + # true. In theory, we could have a branch line with more exits.. + class_branches = 2.0 * len(branch_lines) + missing_branches = sum([len(b) for b in analysis.missing_branch_arcs().values()]) + class_branch_hits = class_branches - missing_branches + else: + class_branches = 0.0 + class_branch_hits = 0.0 + # Finalize the statistics that are collected in the XML DOM. line_rate = rate(class_hits, class_lines) branch_rate = rate(class_branch_hits, class_branches) diff --git a/test/farm/html/gold_y_xml_branch/coverage.xml b/test/farm/html/gold_y_xml_branch/coverage.xml new file mode 100644 index 00000000..96459101 --- /dev/null +++ b/test/farm/html/gold_y_xml_branch/coverage.xml @@ -0,0 +1,22 @@ +<?xml version="1.0" ?>
+<!DOCTYPE coverage
+ SYSTEM 'http://cobertura.sourceforge.net/xml/coverage-03.dtd'>
+<coverage branch-rate="0.5" line-rate="0.8" timestamp="1259288252325" version="3.2b4">
+ <!-- Generated by coverage.py: http://nedbatchelder.com/code/coverage -->
+ <packages>
+ <package branch-rate="0.5" complexity="0.0" line-rate="0.8" name=".">
+ <classes>
+ <class branch-rate="0.5" complexity="0.0" filename="y.py" line-rate="0.8" name="y">
+ <methods/>
+ <lines>
+ <line hits="1" number="3"/>
+ <line branch="true" hits="1" number="4"/>
+ <line hits="1" number="5"/>
+ <line hits="0" number="7"/>
+ <line hits="1" number="9"/>
+ </lines>
+ </class>
+ </classes>
+ </package>
+ </packages>
+</coverage>
diff --git a/test/farm/html/run_y_xml_branch.py b/test/farm/html/run_y_xml_branch.py new file mode 100644 index 00000000..46f426d3 --- /dev/null +++ b/test/farm/html/run_y_xml_branch.py @@ -0,0 +1,20 @@ +def xml_it(): + """Run coverage and make an XML report for y.""" + import coverage + cov = coverage.coverage(branch=True) + cov.start() + import y + cov.stop() + cov.xml_report(y, outfile="../xml_branch/coverage.xml") + +import os +if not os.path.exists("xml_branch"): + os.makedirs("xml_branch") + +runfunc(xml_it, rundir="src") + +compare("gold_y_xml_branch", "xml_branch", scrubs=[ + (r' timestamp="\d+"', ' timestamp="TIMESTAMP"'), + (r' version="[-.\w]+"', ' version="VERSION"'), + ]) +clean("xml_branch") diff --git a/test/farm/html/src/y.py b/test/farm/html/src/y.py new file mode 100644 index 00000000..af7c9689 --- /dev/null +++ b/test/farm/html/src/y.py @@ -0,0 +1,9 @@ +# A test file for XML reporting by coverage. + +def choice(x): + if x < 2: + return 3 + else: + return 4 + +assert choice(1) == 3 |