pymtt
 All Classes Namespaces Files Functions Variables Groups
JunitXML.py
Go to the documentation of this file.
1 # -*- coding: utf-8; tab-width: 4; indent-tabs-mode: f; python-indent: 4 -*-
2 #
3 # Copyright (c) 2015-2018 Intel, Inc. All rights reserved.
4 # $COPYRIGHT$
5 #
6 # Additional copyrights may follow
7 #
8 # $HEADER$
9 #
10 
11 from __future__ import print_function
12 import os
13 import sys
14 import re
15 from junit_xml import TestSuite, TestCase
16 from ReporterMTTStage import *
17 
18 ## @addtogroup Stages
19 # @{
20 # @addtogroup Reporter
21 # @section JunitXML
22 # Junit XML plugin
23 # @param filename Name of the file into which the report is to be written
24 # @param textwrap Max line length before wrapping
25 # @}
27 
28  def __init__(self):
29  # initialise parent class
30  ReporterMTTStage.__init__(self)
31  self.options = {}
32  self.options['filename'] = (None, "Name of the file into which the report is to be written")
33  self.options['textwrap'] = ("80", "Max line length before wrapping")
34  self.fh = sys.stdout
35 
36  def activate(self):
37  # get the automatic procedure from IPlugin
38  IPlugin.activate(self)
39  return
40 
41  def deactivate(self):
42  IPlugin.deactivate(self)
43  return
44 
45  def print_name(self):
46  return "JunitXML"
47 
48  def print_options(self, testDef, prefix):
49  lines = testDef.printOptions(self.options)
50  for line in lines:
51  print(prefix + line)
52  return
53 
54  def execute(self, log, keyvals, testDef):
55  testDef.logger.verbose_print("JunitXML Reporter")
56  # pickup the options
57  cmds = {}
58  testDef.parseOptions(log, self.options, keyvals, cmds)
59  if cmds['filename'] is not None:
60  self.fh = open(cmds['filename'] if os.path.isabs(cmds['filename']) \
61  else os.path.join(cmds['scratch'],cmds['filename']), 'w')
62  if testDef.options['description'] is not None:
63  print(testDef.options['description'], file=self.fh)
64  print(file=self.fh)
65 
66  # Use the Junit classname field to store the list of inifiles
67  try:
68  classname = testDef.log['inifiles']
69  except KeyError:
70  classname = None
71  # get the entire log of results
72  fullLog = testDef.logger.getLog(None)
73  testCases = []
74  # TODO: ain't nobody got time for that. 8-).
75  time = 0
76  for lg in fullLog:
77  if 'stdout' in lg and lg['stdout'] is not None:
78  stdout = "\n".join(lg['stdout'])
79  else:
80  stdout = None
81  if 'stderr' in lg and lg['stderr'] is not None:
82  stderr = "\n".join(lg['stderr'])
83  else:
84  stderr = None
85  if 'time' in lg and lg['time'] is not None:
86  time = lg['time']
87  else:
88  time = 0
89  tc = TestCase(lg['section'], classname, time, stdout, stderr)
90  try:
91  if 0 != lg['status']:
92  # Find sections prefixed with 'TestRun'
93  if re.match("TestRun", lg['section']):
94  tc.add_failure_info("Test reported failure")
95  else:
96  tc.add_error_info("Test error")
97  except KeyError:
98  sys.exit(lg['section'] + " is missing status!")
99  testCases.append(tc)
100 
101  # TODO: Pull in the resource manager jobid.
102  jobid = "job1"
103  ts = TestSuite(jobid, testCases)
104  print(TestSuite.to_xml_string([ts]), file=self.fh)
105 
106  if cmds['filename'] is not None:
107  self.fh.close()
108  log['status'] = 0
109  return