Debian Codegraph @

pbcommand/testkit/nunit.py

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
•1 #!/usr/bin/python3
2
3 """
4 Generate an NUnit XML test report annotated with test issue keys, suitable
5 for importing into JIRA/X-ray
6 """
7
8 # NOTE: deliberately avoiding any dependencies outside the standard library!
9 import argparse
10 import logging
11 import sys
12 from xml.dom import minidom
13
14 log = logging.getLogger(__name__)
15
16
17 class TestCase:
18 """
19 Container for the results of an executed test.
20 """
21
22 def __init__(self, name, success, tests=(), requirements=(),
23 asserts=1):
24 self.name = name
25 self.success = success
26 self.tests = list(tests)
27 self.requirements = list(requirements)
28 self.asserts = asserts
29
30 def to_xml(self, doc):
31 """Create xml.dom.minidom node in document"""
32 test = doc.createElement("test-case")
33 test.setAttribute("name", self.name)
34 test.setAttribute("executed", "True")
35 test.setAttribute("time", "0.001")
36 test.setAttribute("asserts", str(self.asserts))
37 test.setAttribute("success", str(self.success))
38 if self.success:
39 test.setAttribute("result", "Success")
40 else:
41 test.setAttribute("result", "Error")
42 if len(self.tests) > 0 or len(self.requirements) > 0:
43 properties = doc.createElement("properties")
44 for test_key in self.tests:
45 prop = doc.createElement("property")
46 prop.setAttribute("name", "Test")
47 prop.setAttribute("value", test_key)
48 properties.appendChild(prop)
49 for req in self.requirements:
50 prop = doc.createElement("property")
51 prop.setAttribute("name", "Requirement")
52 prop.setAttribute("value", req)
53 properties.appendChild(prop)
54 test.appendChild(properties)
55 return test
56
57 @staticmethod
58 def from_xml(node):
59 tests, requirements = [], []
60 for prop in node.getElementsByTagName("property"):
61 property_type = prop.getAttribute("name")
62 if property_type == "Requirement":
63 requirements.append(prop.getAttribute("value"))
64 elif property_type == "Test":
65 tests.append(prop.getAttribute("value"))
66 return TestCase(
67 name=node.getAttribute("name"),
68 success=node.getAttribute("success") == "True",
69 tests=tests,
70 requirements=requirements,
71 asserts=int(node.getAttribute("asserts")))
72
73
74 def create_nunit_xml(test_cases):
75 """
76 Create overall NUnit XML output for a list of test cases
77 """
78 passed = [t.success for t in test_cases].count(True)
79 failed = [t.success for t in test_cases].count(False)
80 doc = minidom.Document()
81 root = doc.createElement("test-results")
82 root.setAttribute("total", str(passed + failed))
83 root.setAttribute("failed", str(failed))
84 root.setAttribute("passed", str(passed))
85 suite = doc.createElement("test-suite")
86 attr_result = "Success"
87 if failed != 0:
88 attr_result = "Error"
89 root.setAttribute("result", attr_result)
90 suite.setAttribute("result", attr_result)
91 doc.appendChild(root)
92 root.appendChild(suite)
93 results = doc.createElement("results")
94 suite.appendChild(results)
95 for test_case in test_cases:
96 results.appendChild(test_case.to_xml(doc))
97 return doc
98
99
100 def combine_results(xml_files, only_with_properties=False):
101 """
102 Combine multiple NUnit outputs into a single test suite.
103
104 :param xml_files: list of NUnit input file names
105 :param only_with_properties: only output test cases that include one or more property tags (for Bamboo reporting)
106 """
107 test_cases = []
108 for xml_file in xml_files:
109 log.info("Reading NUnit report %s", xml_file)
110 dom = minidom.parse(xml_file)
111 for node in dom.getElementsByTagName("test-case"):
112 if only_with_properties:
113 if len(node.getElementsByTagName("property")) == 0:
114 continue
115 test_cases.append(TestCase.from_xml(node))
116 return create_nunit_xml(test_cases)
117
118
119 def main(argv):
120 """Standalone program runner"""
121 parser = argparse.ArgumentParser(description=__doc__)
122 parser.add_argument("test_names",
123 help="Comma-separated list of test names")
124 parser.add_argument("test_keys",
125 help="JIRA issue(s) associated with this test (comma-separated list)")
126 parser.add_argument("--failed", action="store_false", dest="success",
127 default=True, help="Indicate test failure")
128 parser.add_argument("--output-xml", action="store",
129 default="nunit_out.xml",
130 help="NUnit XML file to write")
131 args = parser.parse_args(argv)
132 test_cases = []
133 for test_name, test_key in zip(args.test_names.split(","),
134 args.test_keys.split(",")):
135 test_cases.append(TestCase(test_name, args.success, [test_key]))
136 doc = create_nunit_xml(test_cases)
137 with open(args.output_xml, "w") as xml_out:
138 xml_out.write(doc.toprettyxml(indent=" "))
139 return 0
140
141
142 if __name__ == "__main__":
143 sys.exit(main(sys.argv[1:]))