Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Formatting of TestCases in the reports

  • Loading branch information...
commit d12ae9a9a3891ca07e4fdd2da611e50d141436b6 1 parent 656df24
@AutomatedTester AutomatedTester authored
Showing with 180 additions and 1 deletion.
  1. +61 −1 garmr.py
  2. +119 −0 test/test_reports.py
View
62 garmr.py
@@ -11,8 +11,68 @@
logger.setLevel(logging.DEBUG)
class Reporter(object):
- pass
+ """
+ This class formats and writes a xUnit style report on the results from
+ the basic tests
+ """
+
+
+ suite_xml="""
+ <?xml version="1.0" encoding="utf-8"?>
+ <testsuite name="Garmr" errors="{error}" failures="{failure}"
+ skips="{skips}" tests="{numtests}" time="{timetaken}">
+ {testresults}
+ </testsuite>
+ """
+
+ def __init__(self, results=None):
+ """
+ Initializes the reporter class
+ Args:
+ results - optional parameter to take the results. If results
+ are not passed in here they need to be passed in
+ write_results method or an exception will be raised
+ """
+ logging.debug("Reporter class initialized")
+ self.results = results
+
+ def write_results(self, file_name='garmr-results.xml', results=None):
+ """
+ This writes the xml to disk.
+ Args:
+ file_name - optional parameter of the name of the file to create
+ results - optional parameter of with the test results. If this is
+ empty and nothing was passed in during object initialization
+ an error will be raised.
+ Note: if you pass in above and here the latest results will
+ be used
+
+ """
+ if self.results is None and results is None:
+ logging.exception("No test results have been passed Reporter")
+ raise Exception("No results have been passed. Please pass in a result")
+
+ def _format_results(self):
+ testcase = """<testcase classname="" name="{testname}" time="{timetaken}">"""
+ formatted_results = ""
+ results = {"time_taken":0,
+ "errors" : 0,
+ "failed" : 0,
+ "skips" : 0}
+ for res in self.results:
+ formatted_results += testcase.format(
+ testname = res["name"],timetaken=res["time_taken"])
+ if res.has_key("errors"):
+ results["errors"] += 1
+ if res.has_key("failed"):
+ results["failed"] += 1
+ if res.has_key("skips"):
+ results["skips"] += 1
+ results["time_taken"] += res["time_taken"]
+
+ results["testcase"] = formatted_results
+ return results
class Garmr(object):
View
119 test/test_reports.py
@@ -0,0 +1,119 @@
+import os
+from garmr import Reporter
+
+class TestReports:
+
+ def setup_method(self, method):
+ if os.path.exists("garmr-results.xml"):
+ os.remove("garmr-results.xml")
+
+ def test_report_throws_exception_with_no_data(self):
+ reporter = Reporter()
+ try:
+ reporter.write_file()
+ raise AssertionError("Exception should have been thrown")
+ except AssertionError as e:
+ raise
+ except Exception:
+ pass
+
+ def test_reporter_formats_test_cases_with_no_errors_or_failures(self):
+ tests_list = []
+ tests_list.append({"name":"good test",
+ "time_taken": 1,
+ })
+ reporter = Reporter(tests_list)
+ result = reporter._format_results()
+ assert result["testcase"] == """<testcase classname="" name="%s" time="%s">""" % \
+ (tests_list[0]["name"], tests_list[0]["time_taken"])
+ assert result["time_taken"] == 1
+ assert result["errors"] == 0
+ assert result["failed"] == 0
+ assert result["skips"] == 0
+
+ def test_reporter_formats_2_test_cases_with_no_errors_or_failures(self):
+ tests_list = []
+ tests_list.append({"name":"good test",
+ "time_taken": 1,
+ })
+ tests_list.append({"name":"good test 2",
+ "time_taken": 1,
+ })
+ reporter = Reporter(tests_list)
+ result = reporter._format_results()
+ expected = """<testcase classname="" name="%s" time="%s">""" % \
+ (tests_list[0]["name"], tests_list[0]["time_taken"])
+ expected += """<testcase classname="" name="%s" time="%s">""" % \
+ (tests_list[1]["name"], tests_list[1]["time_taken"])
+ assert result["testcase"] == expected, result["testcase"]
+ assert result["time_taken"] == 2
+ assert result["errors"] == 0
+ assert result["failed"] == 0
+ assert result["skips"] == 0
+
+ def test_reporter_formats_2_test_cases_with_mix_of_errors_or_failures(self):
+ tests_list = []
+ tests_list.append({"name":"error test",
+ "time_taken": 1,
+ "errors" : True
+ })
+ tests_list.append({"name":"failure test",
+ "time_taken": 1,
+ "failed" : True
+ })
+ reporter = Reporter(tests_list)
+ result = reporter._format_results()
+ expected = """<testcase classname="" name="%s" time="%s">""" % \
+ (tests_list[0]["name"], tests_list[0]["time_taken"])
+ expected += """<testcase classname="" name="%s" time="%s">""" % \
+ (tests_list[1]["name"], tests_list[1]["time_taken"])
+ assert result["testcase"] == expected, result["testcase"]
+ assert result["time_taken"] == 2
+ assert result["errors"] == 1
+ assert result["failed"] == 1
+ assert result["skips"] == 0
+
+ def test_reporter_formats_test_case_with_one_failure(self):
+ tests_list = []
+ tests_list.append({"name":"failedtest",
+ "time_taken": 1,
+ "failed": True,
+ })
+ reporter = Reporter(tests_list)
+ result = reporter._format_results()
+ assert result["testcase"] == """<testcase classname="" name="%s" time="%s">""" % \
+ (tests_list[0]["name"], tests_list[0]["time_taken"])
+ assert result["time_taken"] == 1
+ assert result["errors"] == 0
+ assert result["failed"] == 1
+ assert result["skips"] == 0
+
+ def test_reporter_formats_test_case_with_error(self):
+ tests_list = []
+ tests_list.append({"name":"errorstest",
+ "time_taken": 1,
+ "errors": True,
+ })
+ reporter = Reporter(tests_list)
+ result = reporter._format_results()
+ assert result["testcase"] == """<testcase classname="" name="%s" time="%s">""" % \
+ (tests_list[0]["name"], tests_list[0]["time_taken"])
+ assert result["time_taken"] == 1
+ assert result["errors"] == 1
+ assert result["failed"] == 0
+ assert result["skips"] == 0
+
+ def test_reporter_formats_test_case_with_skip(self):
+ tests_list = []
+ tests_list.append({"name":"skipstest",
+ "time_taken": 1,
+ "skips": True,
+ })
+ reporter = Reporter(tests_list)
+ result = reporter._format_results()
+ assert result["testcase"] == """<testcase classname="" name="%s" time="%s">""" % \
+ (tests_list[0]["name"], tests_list[0]["time_taken"])
+ assert result["time_taken"] == 1
+ assert result["errors"] == 0
+ assert result["failed"] == 0
+ assert result["skips"] == 1
Please sign in to comment.
Something went wrong with that request. Please try again.