利用Tkinter作的自動生成JSONSchema的小工具

      前面講到能夠使用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

再作的自動化點的話也能夠把自動化測試的那些東西填進去, 生成模板修改後接着再繼續自動化使用進行接口測試,那樣能夠作就是會比較重了, 仍是根據本身須要進行相關測試策略設計。工具

相關文章
相關標籤/搜索