Skip to content

Instantly share code, notes, and snippets.

@Lucas-C
Last active March 7, 2018 14:40
Show Gist options
  • Save Lucas-C/2ba14f253186e8df079219a3038e7d94 to your computer and use it in GitHub Desktop.
Save Lucas-C/2ba14f253186e8df079219a3038e7d94 to your computer and use it in GitHub Desktop.
Behave to Cucumber JSON report converter
#!/usr/bin/env python
# USAGE: ./convert_report_to_cucumber_format.py --behave-report bdd/report.json [--json-schema cucumber-report-schema.json]
import argparse, json, sys
from jsonschema import Draft4Validator
def main(argv=None):
args = parse_args(argv)
with open(args.json_schema, 'r') as json_file:
schema = json.load(json_file)
with open(args.behave_report, 'r') as json_file:
report = json.load(json_file)
convert_report(report)
print(json.dumps(report, sort_keys=True, indent=2))
validate_json(schema, report)
def convert_report(report):
# delete_tags param has been introduced for XRay integration purpose
def common_processing(item, delete_tags):
item['uri'], item['line'] = item.pop('location').split(':')
item['line'] = int(item['line'])
if delete_tags:
item['tags'] = []
else:
item['tags'] = [{'name': '@' + tag} for tag in item.get('tags', [])]
if 'id' not in item:
item['id'] = item['name'].replace(' ', '-').lower()
if 'description' in item:
#assert len(item['description']) == 1
item['description'] = item['description'][0]
else:
item['description'] = ''
for feature in report:
common_processing(feature, True)
for scenario in feature['elements']:
common_processing(scenario, False)
for step in scenario['steps']:
step['uri'], step['line'] = step.pop('location').split(':')
step['line'] = int(step['line'])
if 'result' in step:
step['result']['duration'] = int(1000000000 * step['result']['duration'])
else:
step['result'] = {'status': 'skipped', 'duration': 0}
if 'table' in step:
step['rows'] = [{'cells': step['table']['headings']}] + [{'cells': cells} for cells in step['table']['rows']]
del step['table']
if 'match' in step:
if 'arguments' in step['match']:
step['match']['arguments'] = [{'val': '{}'.format(arg['value']), 'offset': 0} for arg in step['match']['arguments']]
else:
step['match'] = {'arguments': [], 'location': 'UNKNOWN - SKIPPED'}
def validate_json(schema, report):
errors = list(Draft4Validator(schema).iter_errors(report))
for error in errors:
print('#/' + '/'.join([str(path) for path in error.path]), error.message, file=sys.stderr)
if errors:
sys.exit(1)
def parse_args(argv):
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('--json-schema', required=True, help=' ')
parser.add_argument('--behave-report', required=True, help=' ')
return parser.parse_args(argv)
if __name__ == '__main__':
main()
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "array",
"minItems": 1,
"items": {
"type": "object",
"properties": {
"comments": {
"type": "array",
"minItems": 1,
"items": {
"type": "object",
"properties": {
"line": {
"type": "integer"
},
"value": {
"type": "string",
"minLength": 1
}
},
"required": [
"value"
]
}
},
"line": {
"type": "integer"
},
"elements": {
"type": "array",
"minItems": 1,
"items": {
"type": "object",
"properties": {
"before": {
"type": "array",
"minItems": 1,
"items": {
"type": "object",
"properties": {
"result": {
"type": "object",
"properties": {
"duration": {
"type": "integer"
},
"status": {
"type": "string",
"minLength": 1
}
},
"required": [
"duration",
"status"
]
},
"match": {
"type": "object",
"properties": {
"location": {
"type": "string",
"minLength": 1
}
},
"required": [
"location"
]
}
},
"required": [
"result",
"match"
]
}
},
"comments": {
"type": "array",
"minItems": 1,
"items": {
"type": "object",
"properties": {
"line": {
"type": "integer"
},
"value": {
"type": "string",
"minLength": 1
}
},
"required": [
"value"
]
}
},
"line": {
"type": "integer"
},
"name": {
"type": "string",
"minLength": 1
},
"description": {
"type": "string"
},
"id": {
"type": "string",
"minLength": 1
},
"after": {
"type": "array",
"minItems": 1,
"items": {
"type": "object",
"properties": {
"result": {
"type": "object",
"properties": {
"duration": {
"type": "integer"
},
"status": {
"type": "string",
"minLength": 1
}
},
"required": [
"duration",
"status"
]
},
"match": {
"type": "object",
"properties": {
"location": {
"type": "string",
"minLength": 1
}
},
"required": [
"location"
]
}
},
"required": [
"result",
"match"
]
}
},
"type": {
"type": "string",
"minLength": 1
},
"keyword": {
"type": "string",
"minLength": 1
},
"steps": {
"type": "array",
"minItems": 1,
"items": {
"type": "object",
"properties": {
"result": {
"type": "object",
"properties": {
"duration": {
"type": "integer"
},
"error_message": {
"type": "string",
"minLength": 1
},
"status": {
"type": "string",
"minLength": 1
}
},
"required": [
"duration",
"status"
]
},
"line": {
"type": "integer"
},
"name": {
"type": "string",
"minLength": 1
},
"description": {
"type": "string"
},
"keyword": {
"type": "string",
"minLength": 1
},
"match": {
"type": "object",
"properties": {
"arguments": {
"type": "array",
"items": {
"type": "object",
"properties": {
"val": {
"type": "string",
"minLength": 1
},
"offset": {
"type": "integer"
}
},
"required": [
"val",
"offset"
]
}
},
"location": {
"type": "string",
"minLength": 1
}
},
"required": [
"arguments",
"location"
]
},
"rows": {
"type": "array",
"minItems": 1,
"items": {
"type": "object",
"properties": {
"cells": {
"type": "array",
"minItems": 1,
"items": {
"type": "string"
}
},
"line": {
"type": "integer"
}
},
"required": [
"cells"
]
}
}
},
"required": [
"result",
"name",
"match"
]
}
},
"tags": {
"type": "array",
"items": {
"type": "object",
"properties": {
"line": {
"type": "integer"
},
"name": {
"type": "string",
"minLength": 1
}
},
"required": [
"name"
]
}
}
},
"required": [
"name",
"description",
"id",
"steps",
"tags",
"type"
]
}
},
"name": {
"type": "string",
"minLength": 1
},
"description": {
"type": "string"
},
"id": {
"type": "string",
"minLength": 1
},
"keyword": {
"type": "string",
"minLength": 1
},
"uri": {
"type": "string",
"minLength": 1
},
"tags": {
"type": "array",
"items": {
"type": "object",
"properties": {
"line": {
"type": "integer"
},
"name": {
"type": "string",
"minLength": 1
}
},
"required": [
"name"
]
}
}
},
"required": [
"elements",
"name",
"description",
"id",
"uri",
"tags"
]
}
}
@President
Copy link

not working for Backgrounds, not working for scenarios without tags

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment