Jpp  debug
the software that should make you happy
Public Member Functions | Static Public Member Functions | Public Attributes | Static Private Member Functions | List of all members
run_tests.TestSuite Class Reference
Inheritance diagram for run_tests.TestSuite:

Public Member Functions

def __init__ (self, name, test_cases=None, hostname=None, id=None, package=None, timestamp=None, properties=None, file=None, log=None, url=None, stdout=None, stderr=None)
 
def build_xml_doc (self, encoding=None)
 

Static Public Member Functions

def to_xml_string (test_suites, prettyprint=True, encoding=None)
 
def to_file (file_descriptor, test_suites, prettyprint=True, encoding=None)
 

Public Attributes

 name
 
 test_cases
 
 timestamp
 
 hostname
 
 id
 
 package
 
 file
 
 log
 
 url
 
 stdout
 
 stderr
 
 properties
 

Static Private Member Functions

def _clean_illegal_xml_chars (string_to_clean)
 

Detailed Description

Suite of test cases.
Can handle unicode strings or binary strings if their encoding is provided.

Definition at line 146 of file run_tests.py.

Constructor & Destructor Documentation

◆ __init__()

def run_tests.TestSuite.__init__ (   self,
  name,
  test_cases = None,
  hostname = None,
  id = None,
  package = None,
  timestamp = None,
  properties = None,
  file = None,
  log = None,
  url = None,
  stdout = None,
  stderr = None 
)

Definition at line 151 of file run_tests.py.

163  stderr=None):
164  self.name = name
165  if not test_cases:
166  test_cases = []
167  try:
168  iter(test_cases)
169  except TypeError:
170  raise Exception('test_cases must be a list of test cases')
171  self.test_cases = test_cases
172  self.timestamp = timestamp
173  self.hostname = hostname
174  self.id = id
175  self.package = package
176  self.file = file
177  self.log = log
178  self.url = url
179  self.stdout = stdout
180  self.stderr = stderr
181  self.properties = properties
182 
General exception.
Definition: Exception.hh:13

Member Function Documentation

◆ build_xml_doc()

def run_tests.TestSuite.build_xml_doc (   self,
  encoding = None 
)
Builds the XML document for the JUnit test suite.
Produces clean unicode strings and decodes non-unicode with the help of encoding.
@param encoding: Used to decode encoded strings.
@return: XML document with unicode string elements

Definition at line 183 of file run_tests.py.

183  def build_xml_doc(self, encoding=None):
184  """
185  Builds the XML document for the JUnit test suite.
186  Produces clean unicode strings and decodes non-unicode with the help of encoding.
187  @param encoding: Used to decode encoded strings.
188  @return: XML document with unicode string elements
189  """
190 
191  # build the test suite element
192  test_suite_attributes = dict()
193  test_suite_attributes['name'] = self.name
194  if any(c.assertions for c in self.test_cases):
195  test_suite_attributes['assertions'] = \
196  str(sum([int(c.assertions) for c in self.test_cases if c.assertions]))
197  test_suite_attributes['disabled'] = \
198  str(len([c for c in self.test_cases if not c.is_enabled]))
199  test_suite_attributes['failures'] = \
200  str(len([c for c in self.test_cases if c.is_failure()]))
201  test_suite_attributes['errors'] = \
202  str(len([c for c in self.test_cases if c.is_error()]))
203  test_suite_attributes['skipped'] = \
204  str(len([c for c in self.test_cases if c.is_skipped()]))
205  test_suite_attributes['time'] = \
206  str(sum(c.elapsed_sec for c in self.test_cases if c.elapsed_sec))
207  test_suite_attributes['tests'] = str(len(self.test_cases))
208 
209  if self.hostname:
210  test_suite_attributes['hostname'] = self.hostname
211  if self.id:
212  test_suite_attributes['id'] = self.id
213  if self.package:
214  test_suite_attributes['package'] = self.package
215  if self.timestamp:
216  test_suite_attributes['timestamp'] = self.timestamp
217  if self.file:
218  test_suite_attributes['file'] = self.file
219  if self.log:
220  test_suite_attributes['log'] = self.log
221  if self.url:
222  test_suite_attributes['url'] = self.url
223 
224  xml_element = ET.Element("testsuite", test_suite_attributes)
225 
226  # add any properties
227  if self.properties:
228  props_element = ET.SubElement(xml_element, "properties")
229  for k, v in self.properties.items():
230  attrs = {
231  'name': k,
232  'value': v
233  }
234  ET.SubElement(props_element, "property", attrs)
235 
236  # add test suite stdout
237  if self.stdout:
238  stdout_element = ET.SubElement(xml_element, "system-out")
239  stdout_element.text = self.stdout
240 
241  # add test suite stderr
242  if self.stderr:
243  stderr_element = ET.SubElement(xml_element, "system-err")
244  stderr_element.text = self.stderr
245 
246  # test cases
247  for case in self.test_cases:
248  test_case_attributes = dict()
249  test_case_attributes['name'] = case.name
250  if case.assertions:
251  # Number of assertions in the test case
252  test_case_attributes['assertions'] = "%d" % case.assertions
253  if case.elapsed_sec:
254  test_case_attributes['time'] = "%f" % case.elapsed_sec
255  if case.timestamp:
256  test_case_attributes['timestamp'] = case.timestamp
257  if case.classname:
258  test_case_attributes['classname'] = case.classname
259  if case.status:
260  test_case_attributes['status'] = case.status
261  if case.category:
262  test_case_attributes['class'] = case.category
263  if case.file:
264  test_case_attributes['file'] = case.file
265  if case.line:
266  test_case_attributes['line'] = case.line
267  if case.log:
268  test_case_attributes['log'] = case.log
269  if case.url:
270  test_case_attributes['url'] = case.url
271 
272  test_case_element = ET.SubElement(xml_element, "testcase",
273  test_case_attributes)
274 
275  # failures
276  if case.is_failure():
277  attrs = {'type': 'failure'}
278  if case.failure_message:
279  attrs['message'] = case.failure_message
280  if case.failure_type:
281  attrs['type'] = case.failure_type
282  failure_element = ET.Element("failure", attrs)
283  if case.failure_output:
284  failure_element.text = case.failure_output
285  test_case_element.append(failure_element)
286 
287  # errors
288  if case.is_error():
289  attrs = {'type': 'error'}
290  if case.error_message:
291  attrs['message'] = case.error_message
292  if case.error_type:
293  attrs['type'] = case.error_type
294  error_element = ET.Element("error", attrs)
295  if case.error_output:
296  error_element.text = case.error_output
297  test_case_element.append(error_element)
298 
299  # skippeds
300  if case.is_skipped():
301  attrs = {'type': 'skipped'}
302  if case.skipped_message:
303  attrs['message'] = case.skipped_message
304  skipped_element = ET.Element("skipped", attrs)
305  if case.skipped_output:
306  skipped_element.text = case.skipped_output
307  test_case_element.append(skipped_element)
308 
309  # test stdout
310  if case.stdout:
311  stdout_element = ET.Element("system-out")
312  stdout_element.text = case.stdout
313  test_case_element.append(stdout_element)
314 
315  # test stderr
316  if case.stderr:
317  stderr_element = ET.Element("system-err")
318  stderr_element.text = case.stderr
319  test_case_element.append(stderr_element)
320 
321  return xml_element
322 

◆ to_xml_string()

def run_tests.TestSuite.to_xml_string (   test_suites,
  prettyprint = True,
  encoding = None 
)
static
Returns the string representation of the JUnit XML document.
@param encoding: The encoding of the input.
@return: unicode string

Definition at line 324 of file run_tests.py.

324  def to_xml_string(test_suites, prettyprint=True, encoding=None):
325  """
326  Returns the string representation of the JUnit XML document.
327  @param encoding: The encoding of the input.
328  @return: unicode string
329  """
330 
331  try:
332  iter(test_suites)
333  except TypeError:
334  raise Exception('test_suites must be a list of test suites')
335 
336  xml_element = ET.Element("testsuites")
337  attributes = defaultdict(int)
338  for ts in test_suites:
339  ts_xml = ts.build_xml_doc(encoding=encoding)
340  for key in ['failures', 'errors', 'tests', 'disabled']:
341  attributes[key] += int(ts_xml.get(key, 0))
342  for key in ['time']:
343  attributes[key] += float(ts_xml.get(key, 0))
344  xml_element.append(ts_xml)
345  for key, value in attributes.items():
346  xml_element.set(key, str(value))
347 
348  xml_string = ET.tostring(xml_element, encoding=encoding)
349  # is encoded now
350  xml_string = TestSuite._clean_illegal_xml_chars(
351  xml_string.decode(encoding or 'utf-8'))
352  # is unicode now
353 
354  if prettyprint:
355  # minidom.parseString() works just on correctly encoded binary strings
356  xml_string = xml_string.encode(encoding or 'utf-8')
357  xml_string = xml.dom.minidom.parseString(xml_string)
358  # toprettyxml() produces unicode if no encoding is being passed or binary string with an encoding
359  xml_string = xml_string.toprettyxml(encoding=encoding)
360  if encoding:
361  xml_string = xml_string.decode(encoding)
362  # is unicode now
363  return xml_string
364 

◆ to_file()

def run_tests.TestSuite.to_file (   file_descriptor,
  test_suites,
  prettyprint = True,
  encoding = None 
)
static
Writes the JUnit XML document to a file.

Definition at line 366 of file run_tests.py.

366  def to_file(file_descriptor, test_suites, prettyprint=True, encoding=None):
367  """
368  Writes the JUnit XML document to a file.
369  """
370  xml_string = TestSuite.to_xml_string(test_suites,
371  prettyprint=prettyprint,
372  encoding=encoding)
373  # has problems with encoded str with non-ASCII (non-default-encoding) characters!
374  file_descriptor.write(xml_string)
375 

◆ _clean_illegal_xml_chars()

def run_tests.TestSuite._clean_illegal_xml_chars (   string_to_clean)
staticprivate
Removes any illegal unicode characters from the given XML string.
@see: http://stackoverflow.com/questions/1707890/fast-way-to-filter-illegal-xml-unicode-chars-in-python

Definition at line 377 of file run_tests.py.

377  def _clean_illegal_xml_chars(string_to_clean):
378  """
379  Removes any illegal unicode characters from the given XML string.
380  @see: http://stackoverflow.com/questions/1707890/fast-way-to-filter-illegal-xml-unicode-chars-in-python
381  """
382 
383  illegal_unichrs = [(0x00, 0x08), (0x0B, 0x1F), (0x7F, 0x84),
384  (0x86, 0x9F), (0xD800, 0xDFFF), (0xFDD0, 0xFDDF),
385  (0xFFFE, 0xFFFF), (0x1FFFE, 0x1FFFF),
386  (0x2FFFE, 0x2FFFF), (0x3FFFE, 0x3FFFF),
387  (0x4FFFE, 0x4FFFF), (0x5FFFE, 0x5FFFF),
388  (0x6FFFE, 0x6FFFF), (0x7FFFE, 0x7FFFF),
389  (0x8FFFE, 0x8FFFF), (0x9FFFE, 0x9FFFF),
390  (0xAFFFE, 0xAFFFF), (0xBFFFE, 0xBFFFF),
391  (0xCFFFE, 0xCFFFF), (0xDFFFE, 0xDFFFF),
392  (0xEFFFE, 0xEFFFF), (0xFFFFE, 0xFFFFF),
393  (0x10FFFE, 0x10FFFF)]
394 
395  illegal_ranges = [
396  "%s-%s" % (unichr(low), unichr(high))
397  for (low, high) in illegal_unichrs if low < sys.maxunicode
398  ]
399 
400  illegal_xml_re = re.compile('[%s]' % ''.join(illegal_ranges))
401  return illegal_xml_re.sub('', string_to_clean)
402 
403 
JRange< T, JComparator_t > join(const JRange< T, JComparator_t > &first, const JRange< T, JComparator_t > &second)
Join ranges.
Definition: JRange.hh:659

Member Data Documentation

◆ name

run_tests.TestSuite.name

Definition at line 164 of file run_tests.py.

◆ test_cases

run_tests.TestSuite.test_cases

Definition at line 171 of file run_tests.py.

◆ timestamp

run_tests.TestSuite.timestamp

Definition at line 172 of file run_tests.py.

◆ hostname

run_tests.TestSuite.hostname

Definition at line 173 of file run_tests.py.

◆ id

run_tests.TestSuite.id

Definition at line 174 of file run_tests.py.

◆ package

run_tests.TestSuite.package

Definition at line 175 of file run_tests.py.

◆ file

run_tests.TestSuite.file

Definition at line 176 of file run_tests.py.

◆ log

run_tests.TestSuite.log

Definition at line 177 of file run_tests.py.

◆ url

run_tests.TestSuite.url

Definition at line 178 of file run_tests.py.

◆ stdout

run_tests.TestSuite.stdout

Definition at line 179 of file run_tests.py.

◆ stderr

run_tests.TestSuite.stderr

Definition at line 180 of file run_tests.py.

◆ properties

run_tests.TestSuite.properties

Definition at line 181 of file run_tests.py.


The documentation for this class was generated from the following file: