Jpp  18.5.2
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 146 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 163 of file run_tests.py.

164  stderr=None):
165  self.name = name
166  if not test_cases:
167  test_cases = []
168  try:
169  iter(test_cases)
170  except TypeError:
171  raise Exception('test_cases must be a list of test cases')
172  self.test_cases = test_cases
173  self.timestamp = timestamp
174  self.hostname = hostname
175  self.id = id
176  self.package = package
177  self.file = file
178  self.log = log
179  self.url = url
180  self.stdout = stdout
181  self.stderr = stderr
182  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 183 of file run_tests.py.

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

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

367  def to_file(file_descriptor, test_suites, prettyprint=True, encoding=None):
368  """
369  Writes the JUnit XML document to a file.
370  """
371  xml_string = TestSuite.to_xml_string(test_suites,
372  prettyprint=prettyprint,
373  encoding=encoding)
374  # has problems with encoded str with non-ASCII (non-default-encoding) characters!
375  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 377 of file run_tests.py.

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

Member Data Documentation

run_tests.TestSuite.name

Definition at line 164 of file run_tests.py.

run_tests.TestSuite.test_cases

Definition at line 171 of file run_tests.py.

run_tests.TestSuite.timestamp

Definition at line 172 of file run_tests.py.

run_tests.TestSuite.hostname

Definition at line 173 of file run_tests.py.

run_tests.TestSuite.id

Definition at line 174 of file run_tests.py.

run_tests.TestSuite.package

Definition at line 175 of file run_tests.py.

run_tests.TestSuite.file

Definition at line 176 of file run_tests.py.

run_tests.TestSuite.log

Definition at line 177 of file run_tests.py.

run_tests.TestSuite.url

Definition at line 178 of file run_tests.py.

run_tests.TestSuite.stdout

Definition at line 179 of file run_tests.py.

run_tests.TestSuite.stderr

Definition at line 180 of file run_tests.py.

run_tests.TestSuite.properties

Definition at line 181 of file run_tests.py.


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