From 4389a97af90c77eed15abb8b1d6bde6b6f175094 Mon Sep 17 00:00:00 2001 From: Hiroki Aramaki Date: Wed, 1 Jul 2015 10:05:42 +0900 Subject: [PATCH] Add failure tag to JUnit report When generate JUnit report, add tag to failed test case. Criterion and detail from sla is also added as type and message with failure tag. Closes-Bug: #1470286 Change-Id: I5def62501b419317eef18045e85626db692628b1 --- rally/cli/commands/task.py | 10 ++++++---- rally/common/junit.py | 18 +++++++++++++----- tests/unit/common/test_junit.py | 12 +++++++----- 3 files changed, 26 insertions(+), 14 deletions(-) diff --git a/rally/cli/commands/task.py b/rally/cli/commands/task.py index 7509747052..0cadb07ec2 100644 --- a/rally/cli/commands/task.py +++ b/rally/cli/commands/task.py @@ -532,6 +532,7 @@ class TaskCommands(object): tasks = isinstance(tasks, list) and tasks or [tasks] results = [] + message = [] processed_names = {} for task_file_or_uuid in tasks: if os.path.exists(os.path.expanduser(task_file_or_uuid)): @@ -585,14 +586,15 @@ class TaskCommands(object): elif out_format == "junit": test_suite = junit.JUnit("Rally test suite") for result in results: - if (isinstance(result["sla"], list) and - not all([sla["success"] for sla in result["sla"]])): + if isinstance(result["sla"], list): + message = ",".join([sla["detail"] for sla in + result["sla"] if not sla["success"]]) + if message: outcome = junit.JUnit.FAILURE else: outcome = junit.JUnit.SUCCESS test_suite.add_test(result["key"]["name"], - result["full_duration"], - outcome=outcome) + result["full_duration"], outcome, message) with open(output_file, "w+") as f: f.write(test_suite.to_xml()) else: diff --git a/rally/common/junit.py b/rally/common/junit.py index 9c36e2c123..4245b888a0 100644 --- a/rally/common/junit.py +++ b/rally/common/junit.py @@ -17,9 +17,9 @@ import xml.etree.ElementTree as ET class JUnit(object): - SUCCESS = 0 - FAILURE = 1 - ERROR = 2 + SUCCESS = "success" + FAILURE = "failure" + ERROR = "error" def __init__(self, test_suite_name): self.test_suite_name = test_suite_name @@ -29,12 +29,14 @@ class JUnit(object): self.n_errors = 0 self.total_time = 0.0 - def add_test(self, test_name, time, outcome=SUCCESS): + def add_test(self, test_name, time, outcome=SUCCESS, message=""): class_name, name = test_name.split(".", 1) self.test_cases.append({ "classname": class_name, "name": name, "time": str("%.2f" % time), + "outcome": outcome, + "message": message }) if outcome == JUnit.FAILURE: @@ -56,5 +58,11 @@ class JUnit(object): "errors": str(self.n_errors), }) for test_case in self.test_cases: - xml.append(ET.Element("testcase", test_case)) + outcome = test_case.pop("outcome") + message = test_case.pop("message") + if outcome in [JUnit.FAILURE, JUnit.ERROR]: + sub = ET.SubElement(xml, "testcase", test_case) + sub.append(ET.Element(outcome, {"message": message})) + else: + xml.append(ET.Element("testcase", test_case)) return ET.tostring(xml, encoding="utf-8").decode("utf-8") diff --git a/tests/unit/common/test_junit.py b/tests/unit/common/test_junit.py index 820ab49890..fda8b5cf5a 100644 --- a/tests/unit/common/test_junit.py +++ b/tests/unit/common/test_junit.py @@ -20,16 +20,18 @@ from tests.unit import test class JUnitTestCase(test.TestCase): def test_basic_testsuite(self): j = junit.JUnit("test") - j.add_test("Foo.Bar", 3.14) - j.add_test("Foo.Baz", 13.37, outcome=junit.JUnit.FAILURE) + j.add_test("Foo.Bar", 3.14, outcome=junit.JUnit.SUCCESS) + j.add_test("Foo.Baz", 13.37, outcome=junit.JUnit.FAILURE, + message="fail_message") j.add_test("Eggs.Spam", 42.00, outcome=junit.JUnit.ERROR) expected = """ - - -""" + + + +""" self.assertEqual(expected.replace("\n", ""), j.to_xml()) def test_empty_testsuite(self):