/usr/lib/python3/dist-packages/behave/reporter/junit.py is in python3-behave 1.2.5-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 | # -*- coding: utf-8 -*-
import os.path
import codecs
import sys
from xml.etree import ElementTree
from behave.reporter.base import Reporter
from behave.model import Scenario, ScenarioOutline, Step
from behave.formatter import ansi_escapes
from behave.model_describe import ModelDescriptor
from behave.textutil import indent, make_indentation, text as _text
import six
def CDATA(text=None):
# -- issue #70: remove_ansi_escapes(text)
element = ElementTree.Element('![CDATA[')
element.text = ansi_escapes.strip_escapes(text)
return element
class ElementTreeWithCDATA(ElementTree.ElementTree):
def _write(self, file, node, encoding, namespaces):
"""This method is for ElementTree <= 1.2.6"""
if node.tag == '![CDATA[':
text = node.text.encode(encoding)
file.write("\n<![CDATA[%s]]>\n" % text)
else:
ElementTree.ElementTree._write(self, file, node, encoding,
namespaces)
if hasattr(ElementTree, '_serialize'):
def _serialize_xml2(write, elem, encoding, qnames, namespaces,
orig=ElementTree._serialize_xml):
if elem.tag == '![CDATA[':
write("\n<%s%s]]>\n" % (elem.tag, elem.text.encode(encoding, "xmlcharrefreplace")))
return
return orig(write, elem, encoding, qnames, namespaces)
def _serialize_xml3(write, elem, qnames, namespaces,
short_empty_elements=None,
orig=ElementTree._serialize_xml):
if elem.tag == '![CDATA[':
write("\n<{tag}{text}]]>\n".format(
tag=elem.tag, text=elem.text))
return
if short_empty_elements:
# python >=3.3
return orig(write, elem, qnames, namespaces, short_empty_elements)
else:
# python <3.3
return orig(write, elem, qnames, namespaces)
if sys.version_info.major == 3:
ElementTree._serialize_xml = \
ElementTree._serialize['xml'] = _serialize_xml3
elif sys.version_info.major == 2:
ElementTree._serialize_xml = \
ElementTree._serialize['xml'] = _serialize_xml2
class FeatureReportData(object):
"""
Provides value object to collect JUnit report data from a Feature.
"""
def __init__(self, feature, filename, classname=None):
if not classname and filename:
classname = filename.replace('/', '.')
self.feature = feature
self.filename = filename
self.classname = classname
self.testcases = []
self.counts_tests = 0
self.counts_errors = 0
self.counts_failed = 0
self.counts_skipped = 0
def reset(self):
self.testcases = []
self.counts_tests = 0
self.counts_errors = 0
self.counts_failed = 0
self.counts_skipped = 0
class JUnitReporter(Reporter):
"""
Generates JUnit-like XML test report for behave.
"""
show_multiline = True
show_timings = True # -- Show step timings.
show_tags = True
def make_feature_filename(self, feature):
filename = None
for path in self.config.paths:
if feature.filename.startswith(path):
filename = feature.filename[len(path) + 1:]
break
if not filename:
# -- NOTE: Directory path (subdirs) are taken into account.
filename = feature.location.relpath(self.config.base_dir)
filename = filename.rsplit('.', 1)[0]
filename = filename.replace('\\', '/').replace('/', '.')
return _text(filename)
# -- REPORTER-API:
def feature(self, feature):
feature_filename = self.make_feature_filename(feature)
classname = feature_filename
report = FeatureReportData(feature, feature_filename)
suite = ElementTree.Element('testsuite')
feature_name = feature.name or feature_filename
suite.set('name', '%s.%s' % (classname, feature_name))
# -- BUILD-TESTCASES: From scenarios
for scenario in feature:
if isinstance(scenario, ScenarioOutline):
scenario_outline = scenario
self._process_scenario_outline(scenario_outline, report)
else:
self._process_scenario(scenario, report)
# -- ADD TESTCASES to testsuite:
for testcase in report.testcases:
suite.append(testcase)
suite.set('tests', _text(report.counts_tests))
suite.set('errors', _text(report.counts_errors))
suite.set('failures', _text(report.counts_failed))
suite.set('skipped', _text(report.counts_skipped)) # WAS: skips
suite.set('time', _text(round(feature.duration, 6)))
if not os.path.exists(self.config.junit_directory):
# -- ENSURE: Create multiple directory levels at once.
os.makedirs(self.config.junit_directory)
tree = ElementTreeWithCDATA(suite)
report_dirname = self.config.junit_directory
report_basename = 'TESTS-%s.xml' % feature_filename
report_filename = os.path.join(report_dirname, report_basename)
tree.write(codecs.open(report_filename, "wb"), "UTF-8")
# -- MORE:
@staticmethod
def select_step_with_status(status, steps):
"""
Helper function to find the first step that has the given step.status.
EXAMPLE: Search for a failing step in a scenario (all steps).
>>> scenario = ...
>>> failed_step = select_step_with_status("failed", scenario)
>>> failed_step = select_step_with_status("failed", scenario.all_steps)
>>> assert failed_step.status == "failed"
EXAMPLE: Search only scenario steps, skip background steps.
>>> failed_step = select_step_with_status("failed", scenario.steps)
:param status: Step status to search for (as string).
:param steps: List of steps to search in (or scenario).
:returns: Step object, if found.
:returns: None, otherwise.
"""
for step in steps:
assert isinstance(step, Step), \
"TYPE-MISMATCH: step.class=%s" % step.__class__.__name__
if step.status == status:
return step
# -- OTHERWISE: No step with the given status found.
# KeyError("Step with status={0} not found".format(status))
return None
@classmethod
def describe_step(cls, step):
status = _text(step.status)
if cls.show_timings:
status += " in %0.3fs" % step.duration
text = '%s %s ... ' % (step.keyword, step.name)
text += '%s\n' % status
if cls.show_multiline:
prefix = make_indentation(2)
if step.text:
text += ModelDescriptor.describe_docstring(step.text, prefix)
elif step.table:
text += ModelDescriptor.describe_table(step.table, prefix)
return text
@classmethod
def describe_tags(cls, tags):
text = ''
if tags:
text = '@'+ ' @'.join(tags)
return text
@classmethod
def describe_scenario(cls, scenario):
"""
Describe the scenario and the test status.
NOTE: table, multiline text is missing in description.
:param scenario: Scenario that was tested.
:return: Textual description of the scenario.
"""
header_line = '\n@scenario.begin\n'
if cls.show_tags and scenario.tags:
header_line += '\n %s\n' % cls.describe_tags(scenario.tags)
header_line += ' %s: %s\n' % (scenario.keyword, scenario.name)
footer_line = '\n@scenario.end\n' + '-' * 80 + '\n'
text = ''
for step in scenario:
text += cls.describe_step(step)
step_indentation = make_indentation(4)
return header_line + indent(text, step_indentation) + footer_line
def _process_scenario(self, scenario, report):
"""
Process a scenario and append information to JUnit report object.
This corresponds to a JUnit testcase:
* testcase.@classname = f(filename) +'.'+ feature.name
* testcase.@name = scenario.name
* testcase.@status = scenario.status
* testcase.@time = scenario.duration
Distinguishes now between failures and errors.
Failures are AssertationErrors: expectation is violated/not met.
Errors are unexpected RuntimeErrors (all other exceptions).
If a failure/error occurs, the step, that caused the failure,
and its location are provided now.
:param scenario: Scenario to process.
:param report: Context object to store/add info to (outgoing param).
"""
assert isinstance(scenario, Scenario)
assert not isinstance(scenario, ScenarioOutline)
report.counts_tests += 1
classname = report.classname
feature = report.feature
feature_name = feature.name
if not feature_name:
feature_name = self.make_feature_filename(feature)
case = ElementTree.Element('testcase')
case.set('classname', '%s.%s' % (classname, feature_name))
case.set('name', scenario.name or '')
case.set('status', scenario.status)
case.set('time', _text(round(scenario.duration, 6)))
step = None
if scenario.status == 'failed':
for status in ('failed', 'undefined'):
step = self.select_step_with_status(status, scenario)
if step:
break
assert step, "OOPS: No failed step found in scenario: %s" % scenario.name
assert step.status in ('failed', 'undefined')
element_name = 'failure'
if isinstance(step.exception, (AssertionError, type(None))):
# -- FAILURE: AssertionError
report.counts_failed += 1
else:
# -- UNEXPECTED RUNTIME-ERROR:
report.counts_errors += 1
element_name = 'error'
# -- COMMON-PART:
failure = ElementTree.Element(element_name)
step_text = self.describe_step(step).rstrip()
text = "\nFailing step: %s\nLocation: %s\n" % (step_text, step.location)
message = _text(step.exception)
if len(message) > 80:
message = message[:80] + "..."
failure.set('type', step.exception.__class__.__name__)
failure.set('message', message)
text += _text(step.error_message)
failure.append(CDATA(text))
case.append(failure)
elif scenario.status in ('skipped', 'untested'):
report.counts_skipped += 1
step = self.select_step_with_status('undefined', scenario)
if step:
# -- UNDEFINED-STEP:
report.counts_failed += 1
failure = ElementTree.Element('failure')
failure.set('type', 'undefined')
failure.set('message', ('Undefined Step: %s' % step.name))
case.append(failure)
else:
skip = ElementTree.Element('skipped')
case.append(skip)
# Create stdout section for each test case
stdout = ElementTree.Element('system-out')
text = self.describe_scenario(scenario)
# Append the captured standard output
if scenario.stdout:
output = _text(scenario.stdout)
text += '\nCaptured stdout:\n%s\n' % output
stdout.append(CDATA(text))
case.append(stdout)
# Create stderr section for each test case
if scenario.stderr:
stderr = ElementTree.Element('system-err')
output = _text(scenario.stderr)
text = '\nCaptured stderr:\n%s\n' % output
stderr.append(CDATA(text))
case.append(stderr)
report.testcases.append(case)
def _process_scenario_outline(self, scenario_outline, report):
assert isinstance(scenario_outline, ScenarioOutline)
for scenario in scenario_outline:
assert isinstance(scenario, Scenario)
self._process_scenario(scenario, report)
|