From 41367a8e8d5990e43a3afc2b3d5ce52933661315 Mon Sep 17 00:00:00 2001 From: Ned Batchelder Date: Wed, 23 Sep 2009 22:11:47 -0400 Subject: Get the test working properly, including adding a usable diff comparison when tests fail. Also make it all work under Py3k. --- coverage/xmlreport.py | 5 ++--- test/farm/html/gold_a_xml/coverage.xml | 18 ------------------ test/farm/html/gold_x_xml/coverage.xml | 18 ++++++++++++++++++ test/farm/html/run_a.py | 2 +- test/farm/html/run_a_xml.py | 17 ----------------- test/farm/html/run_x_xml.py | 17 +++++++++++++++++ test/farm/html/src/x.py | 7 +++++++ test/test_farm.py | 7 ++++--- 8 files changed, 49 insertions(+), 42 deletions(-) delete mode 100644 test/farm/html/gold_a_xml/coverage.xml create mode 100644 test/farm/html/gold_x_xml/coverage.xml delete mode 100644 test/farm/html/run_a_xml.py create mode 100644 test/farm/html/run_x_xml.py create mode 100644 test/farm/html/src/x.py diff --git a/coverage/xmlreport.py b/coverage/xmlreport.py index 7fb2ba6..8d5e4dd 100644 --- a/coverage/xmlreport.py +++ b/coverage/xmlreport.py @@ -3,6 +3,7 @@ import os, sys import xml.dom.minidom +from coverage.backward import sorted from coverage.report import Reporter @@ -103,9 +104,7 @@ class XmlReporter(Reporter): packageXml.appendChild(package) classes = doc.createElement("classes") package.appendChild(classes) - classNames = packageData[1].keys() - classNames.sort() - for className in classNames: + for className in sorted(packageData[1].keys()): classes.appendChild(packageData[1][className]) package.setAttribute("name", packageName.replace(os.sep, '.')) package.setAttribute("line-rate", str(packageData[2]/(packageData[3] or 1.0))) diff --git a/test/farm/html/gold_a_xml/coverage.xml b/test/farm/html/gold_a_xml/coverage.xml deleted file mode 100644 index 9c67a55..0000000 --- a/test/farm/html/gold_a_xml/coverage.xml +++ /dev/null @@ -1,18 +0,0 @@ - - - - - - - - - - - - - - - - - diff --git a/test/farm/html/gold_x_xml/coverage.xml b/test/farm/html/gold_x_xml/coverage.xml new file mode 100644 index 0000000..510590a --- /dev/null +++ b/test/farm/html/gold_x_xml/coverage.xml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + + + + diff --git a/test/farm/html/run_a.py b/test/farm/html/run_a.py index 4a8a051..dc7719a 100644 --- a/test/farm/html/run_a.py +++ b/test/farm/html/run_a.py @@ -11,7 +11,7 @@ runfunc(html_it, rundir="src") # HTML files will change often. Check that the sizes are reasonable, # and check that certain key strings are in the output. -compare("html", "gold_a", size_within=10) +compare("gold_a", "html", size_within=10) contains("html/a.html", "if 1 < 2", "    a = 3", diff --git a/test/farm/html/run_a_xml.py b/test/farm/html/run_a_xml.py deleted file mode 100644 index 6df7dcd..0000000 --- a/test/farm/html/run_a_xml.py +++ /dev/null @@ -1,17 +0,0 @@ -def html_it(): - """Run coverage and make an XML report for a.""" - import coverage - cov = coverage.coverage() - cov.start() - import a - cov.stop() - cov.xml_report(a, outfile=open("../xml/coverage.xml", 'w')) - -import os -if not os.path.exists("xml"): - os.makedirs("xml") - -runfunc(html_it, rundir="src") - -compare("xml", "gold_a_xml") -clean("xml") diff --git a/test/farm/html/run_x_xml.py b/test/farm/html/run_x_xml.py new file mode 100644 index 0000000..c1dbb16 --- /dev/null +++ b/test/farm/html/run_x_xml.py @@ -0,0 +1,17 @@ +def html_it(): + """Run coverage and make an XML report for x.""" + import coverage + cov = coverage.coverage() + cov.start() + import x + cov.stop() + cov.xml_report(x, outfile=open("../xml/coverage.xml", 'w')) + +import os +if not os.path.exists("xml"): + os.makedirs("xml") + +runfunc(html_it, rundir="src") + +compare("gold_x_xml", "xml") +clean("xml") diff --git a/test/farm/html/src/x.py b/test/farm/html/src/x.py new file mode 100644 index 0000000..9e71aeb --- /dev/null +++ b/test/farm/html/src/x.py @@ -0,0 +1,7 @@ +# A test file for HTML reporting by coverage. + +if 1 < 2: + # Needed a < to look at HTML entities. + a = 3 +else: + a = 4 diff --git a/test/test_farm.py b/test/test_farm.py index 6200611..01934df 100644 --- a/test/test_farm.py +++ b/test/test_farm.py @@ -1,6 +1,6 @@ """Run tests in the farm subdirectory. Designed for nose.""" -import filecmp, fnmatch, glob, os, shutil, sys +import difflib, filecmp, fnmatch, glob, os, shutil, sys sys.path.insert(0, os.path.split(__file__)[0]) # Force relative import for Py3k from backtest import run_command, execfile # pylint: disable-msg=W0622 @@ -218,10 +218,11 @@ class FarmTestCase(object): # ourselves. text_diff = [] for f in diff_files: - left = open(os.path.join(dir1, f), "r").read() - right = open(os.path.join(dir2, f), "r").read() + left = open(os.path.join(dir1, f), "r").readlines() + right = open(os.path.join(dir2, f), "r").readlines() if left != right: text_diff.append(f) + print("".join(list(difflib.Differ().compare(left, right)))) assert not text_diff, "Files differ: %s" % text_diff if not left_extra: -- cgit v1.2.1