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
14import sys
15
16# NOTE: The etree has a problem when serializing "newstr" objects,
17# so we have to cast all strings that we get from the outside to str()
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
72 def _addMessage(self, message, msgType, timestamp, file=None, line=None):
73 assert(isinstance(message, str))
74 event = etree.SubElement(self._events, "Event")
75 event.set("type", str(msgType))
76 event.set("timestamp", str(timestamp))
77 if file is not None and line is not None:
78 event.set("file", str(file))
79 event.set("line", str(line))
80 etree.SubElement(event, "Message").text = str(message)
81 return event
82
83
85
86 def __init__(self, testCaseName, package, author, duration, maintainer=None, file=None, line=None, comment=None, showTestFunctionSortingLiterals=False):
87 assert(isinstance(testCaseName, str))
88
89 self._status = 0
90 self._functions = []
91
92 commonAttributes = {"name": str(testCaseName),
93 "type": "FunctionalTestCase"}
94
95 self._xml = etree.Element("ExtraTestCaseResult")
96
97 information = etree.SubElement(self._xml, "Information")
98 testCaseInfo = etree.SubElement(information, "TestCase")
99 infoAttributes = {"timeout": "0"}
100 infoAttributes.update(commonAttributes)
101 for key in infoAttributes:
102 testCaseInfo.set(key, infoAttributes[key])
103 etree.SubElement(testCaseInfo, "Author").text = str(author)
104 etree.SubElement(testCaseInfo, "Package").text = str(package)
105 etree.SubElement(testCaseInfo, "Maintainer").text = str(maintainer) if maintainer else ""
106 etree.SubElement(testCaseInfo, "Comment").text = str(comment) if comment else ""
107 etree.SubElement(testCaseInfo, "File").text = str(file) if file else ""
108 etree.SubElement(testCaseInfo, "Line").text = str(line) if line else ""
109 etree.SubElement(testCaseInfo, "showTestFunctionSortingLiterals").text = "1" if showTestFunctionSortingLiterals else "0"
110
111 self._testFunctions = etree.SubElement(testCaseInfo, "TestFunctions")
112
113 result = etree.SubElement(self._xml, "Result")
114 self._testCaseResult = etree.SubElement(result, "TestCase")
115 resultAttributes = {"retries": "0",
116 "duration": str(duration)}
117 resultAttributes.update(commonAttributes)
118 for key in resultAttributes:
119 self._testCaseResult.set(key, resultAttributes[key])
120
121 def createTestFunction(self, name, duration):
122 basename = name
123 number = len(self._functions)+1
124 if number < 10: number = "00" + str(number)
125 elif number < 100: number = "0" + str(number)
126 else: number = str(number)
127 name = "TEST" + number + "_" + name
128 assert(isinstance(name, str))
129 info = etree.SubElement(self._testFunctions, "Function")
130 info.set("name", str(name))
131 info.set("basename", str(basename))
132 info.set("is_disabled", "False")
133 result = etree.SubElement(self._testCaseResult, "Function")
134 result.set("name", str(name))
135 result.set("duration", str(duration))
136 result.set("is_disabled", "False")
138 self._functions.append(f)
139 return f
140
141 def toXML(self):
142 for f in self._functions:
143 if self._status < f.status():
144 self._status = f.status()
145 self._testCaseResult.set("status", str(self._status))
146 return self._xml
147
148
149from .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)