TestCenter Reference
ExtraTestCaseResult.py
Go to the documentation of this file.
2# Copyright 2012, MeVis Medical Solutions AG
3#
4# The user may use this file in accordance with the license agreement provided with
5# the Software or, alternatively, in accordance with the terms contained in a
6# written agreement between the user and MeVis Medical Solutions AG.
7#
8# For further information use the contact form at https://www.mevislab.de/contact
9#
10
11import xml.etree.cElementTree as etree
12
13import os
14
15# NOTE: The etree has a problem when serializing "newstr" objects,
16# so we have to cast all strings that we get from the outside to str()
17
18
20 INFO = "Info"
21 WARNING = "Warning"
22 ERROR = "Error"
23
24 def __init__(self, xml):
25 self._xml = xml
26 self._status = 0
27 self._xml.set("status", "0")
28 self._xml.set("try", "1")
29 self._events = etree.SubElement(self._xml, "Events")
30
31 def status(self):
32 return self._status
33
34 def info(self, message, timestamp, file=None, line=None):
35 self._addMessage(message, self.INFOINFO, timestamp, file, line)
36
37 def warning(self, message, timestamp, file=None, line=None):
38 self._addMessage(message, self.WARNINGWARNING, timestamp, file, line)
39 if self._status == 0:
40 self._status = 1
41 self._xml.set("status", "1")
42
43 def error(self, message, timestamp, file=None, line=None):
44 self._addMessage(message, self.ERRORERROR, timestamp, file, line)
45 if self._status < 2:
46 self._status = 2
47 self._xml.set("status", "2")
48
49 def showImage(self, description, filenames, timestamp, file=None, line=None):
50 self.showFile(description, filenames, timestamp, file, line, command="ImageShow")
51
52 def showFile(self, description, filenames, timestamp, file=None, line=None, command="FileShow"):
53 if len(filenames) == 0:
54 self.error("No images given!", timestamp)
55 else:
56 fileList = []
57 for filename in filenames:
58 if not os.path.isfile(filename):
59 self.error("File (%s) does not exist" % (filename), timestamp, file, line)
60 else:
61 hash = Base_getHash(filename)
62 fileList.append("%s|%s" % (hash, filename))
63
64 if len(fileList) > 0:
65 event = self._addMessage(description, "Command", timestamp, file, line)
66 event.attrib["command"] = command
67
68 parameters = etree.SubElement(event, "Parameters")
69 parameters.text = ",".join(fileList)
70
71 def _addMessage(self, message, msgType, timestamp, file=None, line=None):
72 assert isinstance(message, str)
73 event = etree.SubElement(self._events, "Event")
74 event.set("type", str(msgType))
75 event.set("timestamp", str(timestamp))
76 if file is not None and line is not None:
77 event.set("file", str(file))
78 event.set("line", str(line))
79 etree.SubElement(event, "Message").text = str(message)
80 return event
81
82
84
86 self,
87 testCaseName,
88 package,
89 author,
90 duration,
91 maintainer=None,
92 file=None,
93 line=None,
94 comment=None,
95 showTestFunctionSortingLiterals=False,
96 ):
97 assert isinstance(testCaseName, str)
98
99 self._status = 0
100 self._functions = []
101
102 commonAttributes = {"name": str(testCaseName), "type": "FunctionalTestCase"}
103
104 self._xml = etree.Element("ExtraTestCaseResult")
105
106 information = etree.SubElement(self._xml, "Information")
107 testCaseInfo = etree.SubElement(information, "TestCase")
108 infoAttributes = {"timeout": "0"}
109 infoAttributes.update(commonAttributes)
110 for key in infoAttributes:
111 testCaseInfo.set(key, infoAttributes[key])
112 etree.SubElement(testCaseInfo, "Author").text = str(author)
113 etree.SubElement(testCaseInfo, "Package").text = str(package)
114 etree.SubElement(testCaseInfo, "Maintainer").text = str(maintainer) if maintainer else ""
115 etree.SubElement(testCaseInfo, "Comment").text = str(comment) if comment else ""
116 etree.SubElement(testCaseInfo, "File").text = str(file) if file else ""
117 etree.SubElement(testCaseInfo, "Line").text = str(line) if line else ""
118 etree.SubElement(testCaseInfo, "showTestFunctionSortingLiterals").text = (
119 "1" if showTestFunctionSortingLiterals else "0"
120 )
121
122 self._testFunctions = etree.SubElement(testCaseInfo, "TestFunctions")
123
124 result = etree.SubElement(self._xml, "Result")
125 self._testCaseResult = etree.SubElement(result, "TestCase")
126 resultAttributes = {"retries": "0", "duration": str(duration)}
127 resultAttributes.update(commonAttributes)
128 for key in resultAttributes:
129 self._testCaseResult.set(key, resultAttributes[key])
130
131 def createTestFunction(self, name, duration):
132 basename = name
133 number = len(self._functions) + 1
134 if number < 10:
135 number = "00" + str(number)
136 elif number < 100:
137 number = "0" + str(number)
138 else:
139 number = str(number)
140 name = "TEST" + number + "_" + name
141 assert isinstance(name, str)
142 info = etree.SubElement(self._testFunctions, "Function")
143 info.set("name", str(name))
144 info.set("basename", str(basename))
145 info.set("is_disabled", "False")
146 result = etree.SubElement(self._testCaseResult, "Function")
147 result.set("name", str(name))
148 result.set("duration", str(duration))
149 result.set("is_disabled", "False")
151 self._functions.append(f)
152 return f
153
154 def toXML(self):
155 for f in self._functions:
156 if self._status < f.status():
157 self._status = f.status()
158 self._testCaseResult.set("status", str(self._status))
159 return self._xml
160
161
162from .Base import getHash as Base_getHash
_addMessage(self, message, msgType, timestamp, file=None, line=None)
error(self, message, timestamp, file=None, line=None)
warning(self, message, timestamp, file=None, line=None)
showFile(self, description, filenames, timestamp, file=None, line=None, command="FileShow")
showImage(self, description, filenames, timestamp, file=None, line=None)
info(self, message, timestamp, file=None, line=None)
__init__(self, testCaseName, package, author, duration, maintainer=None, file=None, line=None, comment=None, showTestFunctionSortingLiterals=False)