Permalink
Browse files

xUnit style report being written to disk

  • Loading branch information...
1 parent d12ae9a commit 29845bcafdaa0cb8fee2d67a9182a38cea1c3a13 @AutomatedTester AutomatedTester committed May 31, 2011
Showing with 46 additions and 5 deletions.
  1. +1 −0 .gitignore
  2. +20 −5 garmr.py
  3. +25 −0 test/test_reports.py
View
@@ -2,3 +2,4 @@
dist/
*.egg-info/
*.pyc
+garmr-results.xml
View
@@ -17,13 +17,11 @@ class Reporter(object):
"""
- suite_xml="""
- <?xml version="1.0" encoding="utf-8"?>
+ suite_xml="""<?xml version="1.0" encoding="utf-8"?>
<testsuite name="Garmr" errors="{error}" failures="{failure}"
skips="{skips}" tests="{numtests}" time="{timetaken}">
{testresults}
- </testsuite>
- """
+ </testsuite>"""
def __init__(self, results=None):
"""
@@ -52,15 +50,32 @@ def write_results(self, file_name='garmr-results.xml', results=None):
logging.exception("No test results have been passed Reporter")
raise Exception("No results have been passed. Please pass in a result")
+ if results is not None:
+ self.results = results
+
+ formatted = self._format_results()
+ suite_results = self.suite_xml.format(error=formatted["errors"],
+ failure=formatted["failed"],
+ skips=formatted["skips"],
+ numtests=formatted["tests"],
+ timetaken=formatted["time_taken"],
+ testresults=formatted["testcase"])
+ file_results = open(file_name, "w")
+ file_results.write(suite_results)
+ file_results.close()
+
+
def _format_results(self):
testcase = """<testcase classname="" name="{testname}" time="{timetaken}">"""
formatted_results = ""
results = {"time_taken":0,
"errors" : 0,
"failed" : 0,
- "skips" : 0}
+ "skips" : 0,
+ "tests" : 0}
for res in self.results:
+ results["tests"] += 1
formatted_results += testcase.format(
testname = res["name"],timetaken=res["time_taken"])
if res.has_key("errors"):
View
@@ -49,6 +49,7 @@ def test_reporter_formats_2_test_cases_with_no_errors_or_failures(self):
assert result["time_taken"] == 2
assert result["errors"] == 0
assert result["failed"] == 0
+ assert result["tests"] == 2
assert result["skips"] == 0
def test_reporter_formats_2_test_cases_with_mix_of_errors_or_failures(self):
@@ -117,3 +118,27 @@ def test_reporter_formats_test_case_with_skip(self):
assert result["errors"] == 0
assert result["failed"] == 0
assert result["skips"] == 1
+
+ def test_that_reporter_writes_to_disk(self):
+ tests_list = []
+ tests_list.append({"name":"skipstest",
+ "time_taken": 1,
+ })
+ suite_xml="""<?xml version="1.0" encoding="utf-8"?>
+ <testsuite name="Garmr" errors="{error}" failures="{failure}"
+ skips="{skips}" tests="{numtests}" time="{timetaken}">
+ {testresults}
+ </testsuite>"""
+ testcase = """<testcase classname="" name="%s" time="%s">""" % \
+ (tests_list[0]["name"], tests_list[0]["time_taken"])
+
+ expected = suite_xml.format(error=0, failure=0, skips=0, numtests=len(tests_list),
+ timetaken=1, testresults=testcase)
+
+ reporter = Reporter(tests_list)
+ reporter.write_results()
+
+ f = open("garmr-results.xml", "r")
+ contents = f.read()
+ f.close()
+ assert expected == contents

0 comments on commit 29845bc

Please sign in to comment.