前面讲到可以使用JSONSchema做json数据校验, 但是每个接口数据都手动写jsonschema太痛苦了, 就写了个小脚本,可以直接复制接口文档的mock数据然后生成一个简单的jsonschema,然后根据需要再修改

#!/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()

效果如下:

简单做了个小Demo, 后续优化, 这样可以直接把生成的jsonschema拿来用了。

再做的自动化点的话也可以把自动化测试的那些东西填进去, 生成模板修改后接着再继续自动化使用进行接口测试,那样可以做就是会比较重了, 还是根据自己需要进行相关测试策略设计。

最新文章

  1. 使用sp_addextendedproperty添加描述信息
  2. C#设计模式——观察者模式(Observer Pattern)
  3. map的相关
  4. 动态加载dll--不占用文件
  5. Binder机制,从Java到C (10. Binder驱动)
  6. 自己封装的tc
  7. SQL常用语句,随时用随时更新
  8. 【题解】UVA11362 Phone List
  9. 分享关于搭建高性能WEB服务器的一篇文章
  10. Git命令面试集
  11. MyEclipse项目的BUG修复错误类型
  12. 解决C#中调用WCF方法报错:远程服务器返回错误 (404) 未找到
  13. MRJob 极速入门教程,使用Python玩转Hadoop
  14. [转载]js 获取ASP RadioButtonList 选中的值
  15. Go语言学习(四)经常使用类型介绍
  16. HTTP --meta详解
  17. PHP中将字符串转化为整数(int) intval() printf()
  18. Adjust Linux Mint Mouse Scroll (Normal/Reverse)
  19. ubuntu问题: 同时只能有一个软件管理工具在运行
  20. 洛谷【P3407】散步

热门文章

  1. atom无法安装插件的解决方法
  2. tensorflow-gpu 1.13 提示找不到 libcublas.so.10.0 的问题
  3. spring boot配置文件
  4. 01 linux上安装 nginx
  5. Yaml 的python 应用
  6. 圆环自带动画进度条ColorfulRingProgressView
  7. python 基础 9.4 游标
  8. 第二课 创建http server
  9. 获取exe文件窗口抓图,将memo转化为JPG输出
  10. 洛谷 P2051 [SDOI2009]学校食堂