Jpp  18.0.0-rc.3
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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__
 
def build_xml_doc
 

Static Public Member Functions

def to_xml_string
 
def to_file
 

Public Attributes

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

Static Private Member Functions

def _clean_illegal_xml_chars
 

Detailed Description

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

Definition at line 164 of file run_tests.py.

Constructor & Destructor Documentation

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 181 of file run_tests.py.

182  stderr=None):
183  self.name = name
184  if not test_cases:
185  test_cases = []
186  try:
187  iter(test_cases)
188  except TypeError:
189  raise Exception('test_cases must be a list of test cases')
190  self.test_cases = test_cases
191  self.timestamp = timestamp
192  self.hostname = hostname
193  self.id = id
194  self.package = package
195  self.file = file
196  self.log = log
197  self.url = url
198  self.stdout = stdout
199  self.stderr = stderr
200  self.properties = properties
General exception.
Definition: Exception.hh:13

Member Function Documentation

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 201 of file run_tests.py.

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

348  def to_xml_string(test_suites, prettyprint=True, encoding=None):
349  """
350  Returns the string representation of the JUnit XML document.
351  @param encoding: The encoding of the input.
352  @return: unicode string
353  """
354 
355  try:
356  iter(test_suites)
357  except TypeError:
358  raise Exception('test_suites must be a list of test suites')
359 
360  xml_element = ET.Element("testsuites")
361  attributes = defaultdict(int)
362  for ts in test_suites:
363  ts_xml = ts.build_xml_doc(encoding=encoding)
364  for key in ['failures', 'errors', 'tests', 'disabled']:
365  attributes[key] += int(ts_xml.get(key, 0))
366  for key in ['time']:
367  attributes[key] += float(ts_xml.get(key, 0))
368  xml_element.append(ts_xml)
369  for key, value in attributes.items():
370  xml_element.set(key, str(value))
371 
372  xml_string = ET.tostring(xml_element, encoding=encoding)
373  # is encoded now
374  xml_string = TestSuite._clean_illegal_xml_chars(
375  xml_string.decode(encoding or 'utf-8'))
376  # is unicode now
377 
378  if prettyprint:
379  # minidom.parseString() works just on correctly encoded binary strings
380  xml_string = xml_string.encode(encoding or 'utf-8')
381  xml_string = xml.dom.minidom.parseString(xml_string)
382  # toprettyxml() produces unicode if no encoding is being passed or binary string with an encoding
383  xml_string = xml_string.toprettyxml(encoding=encoding)
384  if encoding:
385  xml_string = xml_string.decode(encoding)
386  # is unicode now
387  return xml_string
General exception.
Definition: Exception.hh:13
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 389 of file run_tests.py.

390  def to_file(file_descriptor, test_suites, prettyprint=True, encoding=None):
391  """
392  Writes the JUnit XML document to a file.
393  """
394  xml_string = TestSuite.to_xml_string(test_suites,
395  prettyprint=prettyprint,
396  encoding=encoding)
397  # has problems with encoded str with non-ASCII (non-default-encoding) characters!
398  file_descriptor.write(xml_string)
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 400 of file run_tests.py.

401  def _clean_illegal_xml_chars(string_to_clean):
402  """
403  Removes any illegal unicode characters from the given XML string.
404  @see: http://stackoverflow.com/questions/1707890/fast-way-to-filter-illegal-xml-unicode-chars-in-python
405  """
406 
407  illegal_unichrs = [(0x00, 0x08), (0x0B, 0x1F), (0x7F, 0x84),
408  (0x86, 0x9F), (0xD800, 0xDFFF), (0xFDD0, 0xFDDF),
409  (0xFFFE, 0xFFFF), (0x1FFFE, 0x1FFFF),
410  (0x2FFFE, 0x2FFFF), (0x3FFFE, 0x3FFFF),
411  (0x4FFFE, 0x4FFFF), (0x5FFFE, 0x5FFFF),
412  (0x6FFFE, 0x6FFFF), (0x7FFFE, 0x7FFFF),
413  (0x8FFFE, 0x8FFFF), (0x9FFFE, 0x9FFFF),
414  (0xAFFFE, 0xAFFFF), (0xBFFFE, 0xBFFFF),
415  (0xCFFFE, 0xCFFFF), (0xDFFFE, 0xDFFFF),
416  (0xEFFFE, 0xEFFFF), (0xFFFFE, 0xFFFFF),
417  (0x10FFFE, 0x10FFFF)]
418 
419  illegal_ranges = [
420  "%s-%s" % (unichr(low), unichr(high))
421  for (low, high) in illegal_unichrs if low < sys.maxunicode
422  ]
423 
424  illegal_xml_re = re.compile('[%s]' % ''.join(illegal_ranges))
425  return illegal_xml_re.sub('', string_to_clean)
426 
JRange< T, JComparator_t > join(const JRange< T, JComparator_t > &first, const JRange< T, JComparator_t > &second)
Join ranges.
Definition: JRange.hh:659
def _clean_illegal_xml_chars
Definition: run_tests.py:400

Member Data Documentation

run_tests.TestSuite.name

Definition at line 182 of file run_tests.py.

run_tests.TestSuite.test_cases

Definition at line 189 of file run_tests.py.

run_tests.TestSuite.timestamp

Definition at line 190 of file run_tests.py.

run_tests.TestSuite.hostname

Definition at line 191 of file run_tests.py.

run_tests.TestSuite.id

Definition at line 192 of file run_tests.py.

run_tests.TestSuite.package

Definition at line 193 of file run_tests.py.

run_tests.TestSuite.file

Definition at line 194 of file run_tests.py.

run_tests.TestSuite.log

Definition at line 195 of file run_tests.py.

run_tests.TestSuite.url

Definition at line 196 of file run_tests.py.

run_tests.TestSuite.stdout

Definition at line 197 of file run_tests.py.

run_tests.TestSuite.stderr

Definition at line 198 of file run_tests.py.

run_tests.TestSuite.properties

Definition at line 199 of file run_tests.py.


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