json 表示黄瓜JSON报告转换器

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了json 表示黄瓜JSON报告转换器相关的知识,希望对你有一定的参考价值。

#!/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"
    ]
  }
}

以上是关于json 表示黄瓜JSON报告转换器的主要内容,如果未能解决你的问题,请参考以下文章

黄瓜报告json文件中的持续时间单位是啥?

Cucumber Maven:生成测试报告后执行逻辑

如何从量角器[3.0.0]和黄瓜最新版本获得cucumber-test.json

如何在VScode中调试Webdriverio黄瓜框架

CSV和JSON格式相互转换

将nltk树转换为JSON表示