caellion-python-commons
parse-tests.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 import xml.etree.ElementTree as ET
3 import os
4 import shutil
5 
6 if os.path.isfile("reports/mutmut.xml"):
7  tree = ET.parse("reports/mutmut.xml")
8  root = tree.getroot()
9  # findallres = root.findall("{http://microsoft.com/schemas/VisualStudio/TeamTest/2010}TestDefinitions")
10  for element in root: # findallres[0]:
11 
12  if element.tag == "testsuite":
13  element.set("name", "mutmut")
14 
15  # print(element.tag, element.attrib)
16  for element2 in element:
17 
18  if element2.tag == "testcase":
19  filename = element2.get("file")
20  name = element2.get("name")
21  name = name.replace("Mutant #", "")
22  filename_split = filename.split("/")
23  filename_split[len(filename_split) - 1] = filename_split[len(filename_split) - 1].replace(".py", "")
24  classname = "Mutations." + ".".join(filename_split)
25 
26  element2.set("classname", classname)
27  element2.set("name", name)
28 
29  # print(element2.tag, element2.attrib)
30  # ET.dump(tree)
31  tree.write("reports/mutmut.xml", encoding="UTF-8", xml_declaration=False)
32 
33 if os.path.isfile("reports/nose2-junit.xml"):
34  tree = ET.parse("reports/nose2-junit.xml")
35  root = tree.getroot()
36  # findallres = root.findall("{http://microsoft.com/schemas/VisualStudio/TeamTest/2010}TestDefinitions")
37  for element in root: # findallres[0]:
38  if element.tag == "testcase":
39  classname = element.get("classname")
40  classname = classname.split(".")
41  del classname[0]
42  classname = "Tests." + ".".join(classname)
43 
44  element.set("classname", classname)
45  # print(element.tag, element.attrib)
46  # ET.dump(tree)
47  tree.write("reports/nose2-junit.xml", encoding="UTF-8", xml_declaration=False)
48 
49 
50 total_mutations = 0
51 killed_mutations = 0
52 timeout_mutations = 0
53 survived_mutations = 0
54 
55 if os.path.isfile("reports/mutmut.xml"):
56  newtree = ET.Element("mutations")
57  tree = ET.parse("reports/mutmut.xml")
58  root = tree.getroot()
59  for element in root:
60  for element2 in element:
61  total_mutations += 1
62  status = "KILLED"
63  detected = "true"
64  diff = ""
65  children = list(element2)
66  this_survived = False
67  for x in children:
68  if x.tag == "failure":
69  msg = x.get("message")
70  if msg == "bad_survived":
71  status = "SURVIVED"
72  detected = "false"
73  diff = x.text
74  survived_mutations += 1
75  if not this_survived:
76  this_survived = True
77  else:
78  print(msg)
79  if x.tag == "error":
80  msg = x.get("message")
81  if msg == "bad_timeout":
82  status = "TIMED_OUT"
83  detected = "false"
84  diff = x.text
85  timeout_mutations += 1
86  if not this_survived:
87  this_survived = True
88  elif msg == "untested":
89  status = "NO_COVERAGE"
90  detected = "false"
91  diff = x.text
92  timeout_mutations += 1
93  if not this_survived:
94  this_survived = True
95  else:
96  print(msg)
97 
98  if not this_survived:
99  killed_mutations += 1
100 
101  addelem = ET.Element("mutation")
102  addelem.set("detected", detected)
103  addelem.set("status", status)
104 
105  sourceFile = ET.Element("sourceFile")
106  sourceFile.text = element2.get("file")
107 
108  lineNumber = ET.Element("lineNumber")
109  lineNumber.text = element2.get("line")
110 
111  index = ET.Element("index")
112  index.text = element2.get("name")
113 
114  description = ET.Element("description")
115  description.text = diff
116 
117  mutatedClass = ET.Element("mutatedClass")
118  classname = element2.get("file").split(".")[0]
119  mutatedClass.text = classname.replace("/", ".")
120 
121  mutatedMethod = ET.Element("mutatedMethod")
122  mutatedMethod.text = "mutation_" + element2.get("name")
123 
124  methodDescription = ET.Element("methodDescription")
125  methodDescription.text = "mutation_" + element2.get("name")
126 
127  addelem.append(sourceFile)
128  addelem.append(mutatedClass)
129  addelem.append(mutatedMethod)
130  addelem.append(methodDescription)
131  addelem.append(lineNumber)
132  addelem.append(ET.Element("mutator"))
133  addelem.append(index)
134  addelem.append(ET.Element("killingTest"))
135  addelem.append(description)
136 
137  newtree.append(addelem)
138  newtree = ET.ElementTree(newtree)
139  newtree.write("reports/mutation-report-null/mutations.xml", encoding="UTF-8", xml_declaration=True)
140 
141 with open("reports/mut-total", "w") as fout:
142  fout.write(str(total_mutations))
143 with open("reports/mut-killed", "w") as fout:
144  fout.write(str(killed_mutations))
145 with open("reports/mut-survived", "w") as fout:
146  fout.write(str(survived_mutations))
147 with open("reports/mut-timeout", "w") as fout:
148  fout.write(str(timeout_mutations))
149 
150 # Prepare HTML report folder
151 basepath = "reports/mutation-report-null/"
152 # step 1: rename all files
153 for root, subdirs, files in os.walk(basepath):
154  if root != basepath and len(files) > 0:
155  subpath = root.replace(basepath, "")
156 
157  if "." not in subpath:
158  path_to_make = subpath.replace("/", ".")
159  if not os.path.exists(basepath + path_to_make):
160  os.makedirs(basepath + path_to_make, exist_ok=True)
161 
162  for x in files:
163  filename_base = root + "/" + x
164  filename_new = basepath + path_to_make + "/" + x.replace(".py.html", ".java.html")
165  shutil.copy(filename_base, filename_new)