前面讲到能够使用JSONSchema作json数据校验, 可是每一个接口数据都手动写jsonschema太痛苦了, 就写了个小脚本,能够直接复制接口文档的mock数据而后生成一个简单的jsonschema,而后根据须要再修改python
#!/usr/bin/env python # -*- coding: utf-8 -*- # @File : jsonUtil.py # @Author: Lcy # @Date : 2018/8/16 # @Desc : from tkinter import * import json class My_GUI(): def __init__(self): self.window = Tk() # 设置主窗口属性(窗口名, 大小, 位置, 背景色等) self.window.title('JSON处理工具') # window.geometry('800x600+50+50') self.window['bg'] = 'GhostWhite' self.window.attributes('-alpha', 1) #添加两个文本框 self.input_text = Text(self.window) self.input_text['bg'] = 'pink' self.input_text.grid(row=0, column=0, sticky=W) self.result_labe = Text(self.window) self.result_labe['bg'] = 'blue' self.result_labe.grid(row=0, column=2) self.to_json_button = Button(self.window, text='to_json', bg='lightblue', width=10, command=self.to_json) self.to_json_button.grid(row=1, column=1) self.window.mainloop() # def set_window(self, window): def to_json(self): #置空text self.result_labe.delete(0.0, END) def to_jsonschema(json_data, result): ''' 递归生成jsonschema :return: ''' if isinstance(json_data, dict): is_null = True result.append('{') result.append("'type': 'object',") result.append("'properties': {") for k, v in json_data.items(): is_null = False result.append("'%s':" % k) to_jsonschema(v, result) result.append(',') if not is_null: result.pop() result.append('}') result.append('}') elif isinstance(json_data, list): result.append('{') result.append("'type': 'array',") result.append("'items': ") to_jsonschema(json_data[0], result) result.append('}') elif isinstance(json_data, int): result.append("{") result.append("'type': 'number'") result.append('}') elif isinstance(json_data, str): result.append("{") result.append("'type': 'string'") result.append('}') return "".join(result) json_data = self.input_text.get(0.0, END).strip().replace("\n", "") result = [] try: testdata = to_jsonschema(eval(json_data), result) params = eval(testdata) self.result_labe.insert(1.0, json.dumps(params, indent=4)) except Exception as e: self.result_labe.insert(1.0, '输入的JSON数据有问题, 请检查') my_gui = My_GUI()
效果以下:json
简单作了个小Demo, 后续优化, 这样能够直接把生成的jsonschema拿来用了。app
再作的自动化点的话也能够把自动化测试的那些东西填进去, 生成模板修改后接着再继续自动化使用进行接口测试,那样能够作就是会比较重了, 仍是根据本身须要进行相关测试策略设计。工具