TestCenter Reference
ExtraTestCaseResult.py
Go to the documentation of this file.
1 #
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 
11 from builtins import object
12 
13 import xml.etree.cElementTree as etree
14 
15 import os
16 import sys
17 import six
18 
19 # NOTE: The etree has a problem when serializing "newstr" objects,
20 # so we have to cast all strings that we get from the outside to unicode()
21 if six.PY3:
22  unicode = str
23 
25  INFO = "Info"
26  WARNING = "Warning"
27  ERROR = "Error"
28 
29  def __init__(self, xml):
30  self._xml_xml = xml
31  self._status_status = 0
32  self._xml_xml.set("status", "0")
33  self._xml_xml.set("try", "1")
34  self._events_events = etree.SubElement(self._xml_xml, "Events")
35 
36  def status(self):
37  return self._status_status
38 
39  def info(self, message, timestamp, file=None, line=None):
40  self._addMessage_addMessage(message, self.INFOINFO, timestamp, file, line)
41 
42  def warning(self, message, timestamp, file=None, line=None):
43  self._addMessage_addMessage(message, self.WARNINGWARNING, timestamp, file, line)
44  if self._status_status == 0:
45  self._status_status = 1
46  self._xml_xml.set("status", "1")
47 
48  def error(self, message, timestamp, file=None, line=None):
49  self._addMessage_addMessage(message, self.ERRORERROR, timestamp, file, line)
50  if self._status_status < 2:
51  self._status_status = 2
52  self._xml_xml.set("status", "2")
53 
54  def showImage(self, description, filenames, timestamp, file=None, line=None):
55  self.showFileshowFile(description, filenames, timestamp, file, line, command="ImageShow")
56 
57  def showFile(self, description, filenames, timestamp, file=None, line=None, command="FileShow"):
58  if len(filenames) == 0:
59  self.errorerror("No images given!", timestamp)
60  else:
61  fileList = []
62  for filename in filenames:
63  if not os.path.isfile(filename):
64  self.errorerror("File (%s) does not exist" % (filename), timestamp, file, line)
65  else:
66  hash = Base_getHash(filename)
67  fileList.append("%s|%s" % (hash, filename))
68 
69  if len(fileList) > 0:
70  event = self._addMessage_addMessage(description, "Command", timestamp, file, line)
71  event.attrib["command"] = command
72 
73  parameters = etree.SubElement(event, "Parameters")
74  parameters.text = ",".join(fileList)
75 
76 
77  def _addMessage(self, message, msgType, timestamp, file=None, line=None):
78  assert(isinstance(message, six.string_types))
79  event = etree.SubElement(self._events_events, "Event")
80  event.set("type", unicode(msgType))
81  event.set("timestamp", unicode(timestamp))
82  if file is not None and line is not None:
83  event.set("file", unicode(file))
84  event.set("line", unicode(line))
85  etree.SubElement(event, "Message").text = unicode(message)
86  return event
87 
88 
89 class ExtraTestCaseResult(object):
90 
91  def __init__(self, testCaseName, package, author, duration, maintainer=None, file=None, line=None, comment=None, showTestFunctionSortingLiterals=False):
92  assert(isinstance(testCaseName, six.string_types))
93 
94  self._status_status = 0
95  self._functions_functions = []
96 
97  commonAttributes = {"name": unicode(testCaseName),
98  "type": "FunctionalTestCase"}
99 
100  self._xml_xml = etree.Element("ExtraTestCaseResult")
101 
102  information = etree.SubElement(self._xml_xml, "Information")
103  testCaseInfo = etree.SubElement(information, "TestCase")
104  infoAttributes = {"timeout": "0"}
105  infoAttributes.update(commonAttributes)
106  for key in infoAttributes:
107  testCaseInfo.set(key, infoAttributes[key])
108  etree.SubElement(testCaseInfo, "Author").text = unicode(author)
109  etree.SubElement(testCaseInfo, "Package").text = unicode(package)
110  etree.SubElement(testCaseInfo, "Maintainer").text = unicode(maintainer) if maintainer else ""
111  etree.SubElement(testCaseInfo, "Comment").text = unicode(comment) if comment else ""
112  etree.SubElement(testCaseInfo, "File").text = unicode(file) if file else ""
113  etree.SubElement(testCaseInfo, "Line").text = unicode(line) if line else ""
114  etree.SubElement(testCaseInfo, "showTestFunctionSortingLiterals").text = "1" if showTestFunctionSortingLiterals else "0"
115 
116  self._testFunctions_testFunctions = etree.SubElement(testCaseInfo, "TestFunctions")
117 
118  result = etree.SubElement(self._xml_xml, "Result")
119  self._testCaseResult_testCaseResult = etree.SubElement(result, "TestCase")
120  resultAttributes = {"retries": "0",
121  "duration": unicode(duration)}
122  resultAttributes.update(commonAttributes)
123  for key in resultAttributes:
124  self._testCaseResult_testCaseResult.set(key, resultAttributes[key])
125 
126  def createTestFunction(self, name, duration):
127  basename = name
128  number = len(self._functions_functions)+1
129  if number < 10: number = "00" + str(number)
130  elif number < 100: number = "0" + str(number)
131  else: number = str(number)
132  name = "TEST" + number + "_" + name
133  assert(isinstance(name, six.string_types))
134  info = etree.SubElement(self._testFunctions_testFunctions, "Function")
135  info.set("name", unicode(name))
136  info.set("basename", unicode(basename))
137  info.set("is_disabled", "False")
138  result = etree.SubElement(self._testCaseResult_testCaseResult, "Function")
139  result.set("name", unicode(name))
140  result.set("duration", unicode(duration))
141  result.set("is_disabled", "False")
142  f = ExtraTestCaseResultFunction(result)
143  self._functions_functions.append(f)
144  return f
145 
146  def toXML(self):
147  for f in self._functions_functions:
148  if self._status_status < f.status():
149  self._status_status = f.status()
150  self._testCaseResult_testCaseResult.set("status", unicode(self._status_status))
151  return self._xml_xml
152 
153 
154 from .Base import getHash as Base_getHash
def info(self, message, timestamp, file=None, line=None)
def showFile(self, description, filenames, timestamp, file=None, line=None, command="FileShow")
def _addMessage(self, message, msgType, timestamp, file=None, line=None)
def error(self, message, timestamp, file=None, line=None)
def showImage(self, description, filenames, timestamp, file=None, line=None)
def warning(self, message, timestamp, file=None, line=None)
def __init__(self, testCaseName, package, author, duration, maintainer=None, file=None, line=None, comment=None, showTestFunctionSortingLiterals=False)