Robot Framework Integrated Development Environment (RIDE)
resultwriter.py
Go to the documentation of this file.
1 # Copyright 2008-2015 Nokia Networks
2 # Copyright 2016- Robot Framework Foundation
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 
16 from robotide.lib.robot.conf import RebotSettings
17 from robotide.lib.robot.errors import DataError
18 from robotide.lib.robot.model import ModelModifier
19 from robotide.lib.robot.output import LOGGER
20 from robotide.lib.robot.result import ExecutionResult, Result
21 from robotide.lib.robot.utils import unic
22 
23 from .jsmodelbuilders import JsModelBuilder
24 from .logreportwriters import LogWriter, ReportWriter
25 from .xunitwriter import XUnitWriter
26 
27 
28 
41 class ResultWriter():
42 
43  def __init__(self, *sources):
44  self._sources_sources = sources
45 
46 
54  def write_results(self, settings=None, **options):
55  settings = settings or RebotSettings(options)
56  results = Results(settings, *self._sources_sources)
57  if settings.output:
58  self._write_output_write_output(results.result, settings.output)
59  if settings.xunit:
60  self._write_xunit_write_xunit(results.result, settings.xunit,
61  settings.xunit_skip_noncritical)
62  if settings.log:
63  config = dict(settings.log_config,
64  minLevel=results.js_result.min_level)
65  self._write_log_write_log(results.js_result, settings.log, config)
66  if settings.report:
67  results.js_result.remove_data_not_needed_in_report()
68  self._write_report_write_report(results.js_result, settings.report,
69  settings.report_config)
70  return results.return_code
71 
72  def _write_output(self, result, path):
73  self._write_write('Output', result.save, path)
74 
75  def _write_xunit(self, result, path, skip_noncritical):
76  self._write_write('XUnit', XUnitWriter(result, skip_noncritical).write, path)
77 
78  def _write_log(self, js_result, path, config):
79  self._write_write('Log', LogWriter(js_result).write, path, config)
80 
81  def _write_report(self, js_result, path, config):
82  self._write_write('Report', ReportWriter(js_result).write, path, config)
83 
84  def _write(self, name, writer, path, *args):
85  try:
86  writer(path, *args)
87  except DataError as err:
88  LOGGER.error(err.message)
89  except EnvironmentError as err:
90  # `err.filename` can be different than `path` at least if reading
91  # log/report templates or writing split log fails.
92  # `unic` is needed due to http://bugs.jython.org/issue1825.
93  LOGGER.error("Writing %s file '%s' failed: %s: %s" %
94  (name.lower(), path, err.strerror, unic(err.filename)))
95  else:
96  LOGGER.output_file(name, path)
97 
98 
99 class Results():
100 
101  def __init__(self, settings, *sources):
102  self._settings_settings = settings
103  self._sources_sources = sources
104  if len(sources) == 1 and isinstance(sources[0], Result):
105  self._result_result = sources[0]
106  self._prune_prune = False
107  self.return_codereturn_code = self._result_result.return_code
108  else:
109  self._result_result = None
110  self._prune_prune = True
111  self.return_codereturn_code = -1
112  self._js_result_js_result = None
113 
114  @property
115  result = property
116 
117  def result(self):
118  if self._result_result is None:
119  include_keywords = bool(self._settings_settings.log or self._settings_settings.output)
120  flattened = self._settings_settings.flatten_keywords
121  self._result_result = ExecutionResult(include_keywords=include_keywords,
122  flattened_keywords=flattened,
123  merge=self._settings_settings.merge,
124  rpa=self._settings_settings.rpa,
125  *self._sources_sources)
126  if self._settings_settings.rpa is None:
127  self._settings_settings.rpa = self._result_result.rpa
128  self._result_result.configure(self._settings_settings.status_rc,
129  self._settings_settings.suite_config,
130  self._settings_settings.statistics_config)
131  modifier = ModelModifier(self._settings_settings.pre_rebot_modifiers,
132  self._settings_settings.process_empty_suite,
133  LOGGER)
134  self._result_result.suite.visit(modifier)
135  self.return_codereturn_code = self._result_result.return_code
136  return self._result_result
137 
138  @property
139  js_result = property
140 
141  def js_result(self):
142  if self._js_result_js_result is None:
143  builder = JsModelBuilder(log_path=self._settings_settings.log,
144  split_log=self._settings_settings.split_log,
145  prune_input_to_save_memory=self._prune_prune)
146  self._js_result_js_result = builder.build_from(self.resultresultresult)
147  if self._prune_prune:
148  self._result_result = None
149  return self._js_result_js_result
A class to create log, report, output XML and xUnit files.
Definition: resultwriter.py:41
def write_results(self, settings=None, **options)
Writes results based on the given settings or options.
Definition: resultwriter.py:54
def _write(self, name, writer, path, *args)
Definition: resultwriter.py:84
def _write_report(self, js_result, path, config)
Definition: resultwriter.py:81
def _write_xunit(self, result, path, skip_noncritical)
Definition: resultwriter.py:75
def _write_log(self, js_result, path, config)
Definition: resultwriter.py:78
def ExecutionResult(*sources, **options)
Factory method to constructs :class:~.executionresult.Result objects.