/usr/lib/python3/dist-packages/behave/formatter/json.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 | # -*- coding: utf-8 -*-
from behave.formatter.base import Formatter
import base64
import six
try:
import json
except ImportError:
import simplejson as json
# -----------------------------------------------------------------------------
# CLASS: JSONFormatter
# -----------------------------------------------------------------------------
class JSONFormatter(Formatter):
name = 'json'
description = 'JSON dump of test run'
dumps_kwargs = {}
split_text_into_lines = True # EXPERIMENT for better readability.
json_number_types = six.integer_types + (float,)
json_scalar_types = json_number_types + (six.text_type, bool, type(None))
def __init__(self, stream_opener, config):
super(JSONFormatter, self).__init__(stream_opener, config)
# -- ENSURE: Output stream is open.
self.stream = self.open()
self.feature_count = 0
self.current_feature = None
self.current_feature_data = None
self._step_index = 0
def reset(self):
self.current_feature = None
self.current_feature_data = None
self._step_index = 0
# -- FORMATTER API:
def uri(self, uri):
pass
def feature(self, feature):
self.reset()
self.current_feature = feature
self.current_feature_data = {
'keyword': feature.keyword,
'name': feature.name,
'tags': list(feature.tags),
'location': six.text_type(feature.location),
'status': feature.status,
}
element = self.current_feature_data
if feature.description:
element['description'] = feature.description
def background(self, background):
element = self.add_feature_element({
'type': 'background',
'keyword': background.keyword,
'name': background.name,
'location': six.text_type(background.location),
'steps': [],
})
if background.name:
element['name'] = background.name
self._step_index = 0
# -- ADD BACKGROUND STEPS: Support *.feature file regeneration.
for step_ in background.steps:
self.step(step_)
def scenario(self, scenario):
element = self.add_feature_element({
'type': 'scenario',
'keyword': scenario.keyword,
'name': scenario.name,
'tags': scenario.tags,
'location': six.text_type(scenario.location),
'steps': [],
})
if scenario.description:
element['description'] = scenario.description
self._step_index = 0
def scenario_outline(self, scenario_outline):
element = self.add_feature_element({
'type': 'scenario_outline',
'keyword': scenario_outline.keyword,
'name': scenario_outline.name,
'tags': scenario_outline.tags,
'location': six.text_type(scenario_outline.location),
'steps': [],
'examples': [],
})
if scenario_outline.description:
element['description'] = scenario_outline.description
self._step_index = 0
@classmethod
def make_table(cls, table):
table_data = {
'headings': table.headings,
'rows': [ list(row) for row in table.rows ]
}
return table_data
def examples(self, examples):
e = {
'type': 'examples',
'keyword': examples.keyword,
'name': examples.name,
'location': six.text_type(examples.location),
}
if examples.table:
e['table'] = self.make_table(examples.table)
element = self.current_feature_element
element['examples'].append(e)
def step(self, step):
s = {
'keyword': step.keyword,
'step_type': step.step_type,
'name': step.name,
'location': six.text_type(step.location),
}
if step.text:
text = step.text
if self.split_text_into_lines and "\n" in text:
text = text.splitlines()
s['text'] = text
if step.table:
s['table'] = self.make_table(step.table)
element = self.current_feature_element
element['steps'].append(s)
def match(self, match):
args = []
for argument in match.arguments:
argument_value = argument.value
if not isinstance(argument_value, self.json_scalar_types):
# -- OOPS: Avoid invalid JSON format w/ custom types.
# Use raw string (original) instead.
argument_value = argument.original
assert isinstance(argument_value, self.json_scalar_types)
arg = {
'value': argument_value,
}
if argument.name:
arg['name'] = argument.name
if argument.original != argument_value:
# -- REDUNDANT DATA COMPRESSION: Suppress for strings.
arg['original'] = argument.original
args.append(arg)
match_data = {
'location': six.text_type(match.location) or "",
'arguments': args,
}
if match.location:
# -- NOTE: match.location=None occurs for undefined steps.
steps = self.current_feature_element['steps']
steps[self._step_index]['match'] = match_data
def result(self, result):
steps = self.current_feature_element['steps']
steps[self._step_index]['result'] = {
'status': result.status,
'duration': result.duration,
}
if result.error_message and result.status == 'failed':
# -- OPTIONAL: Provided for failed steps.
error_message = result.error_message
if self.split_text_into_lines and "\n" in error_message:
error_message = error_message.splitlines()
result_element = steps[self._step_index]['result']
result_element['error_message'] = error_message
self._step_index += 1
def embedding(self, mime_type, data):
step = self.current_feature_element['steps'][-1]
step['embeddings'].append({
'mime_type': mime_type,
'data': base64.b64encode(data).replace('\n', ''),
})
def eof(self):
"""
End of feature
"""
if not self.current_feature_data:
return
# -- NORMAL CASE: Write collected data of current feature.
self.update_status_data()
if self.feature_count == 0:
# -- FIRST FEATURE:
self.write_json_header()
else:
# -- NEXT FEATURE:
self.write_json_feature_separator()
self.write_json_feature(self.current_feature_data)
self.current_feature_data = None
self.feature_count += 1
def close(self):
self.write_json_footer()
self.close_stream()
# -- JSON-DATA COLLECTION:
def add_feature_element(self, element):
assert self.current_feature_data is not None
if 'elements' not in self.current_feature_data:
self.current_feature_data['elements'] = []
self.current_feature_data['elements'].append(element)
return element
@property
def current_feature_element(self):
assert self.current_feature_data is not None
return self.current_feature_data['elements'][-1]
def update_status_data(self):
assert self.current_feature
assert self.current_feature_data
self.current_feature_data['status'] = self.current_feature.status
# -- JSON-WRITER:
def write_json_header(self):
self.stream.write('[\n')
def write_json_footer(self):
self.stream.write('\n]\n')
def write_json_feature(self, feature_data):
self.stream.write(json.dumps(feature_data, **self.dumps_kwargs))
self.stream.flush()
def write_json_feature_separator(self):
self.stream.write(",\n\n")
# -----------------------------------------------------------------------------
# CLASS: PrettyJSONFormatter
# -----------------------------------------------------------------------------
class PrettyJSONFormatter(JSONFormatter):
"""
Provides readable/comparable textual JSON output.
"""
name = 'json.pretty'
description = 'JSON dump of test run (human readable)'
dumps_kwargs = { 'indent': 2, 'sort_keys': True }
|