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


      前面講到可以使用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拿來用了。

再做的自動化點的話也可以把自動化測試的那些東西填進去, 生成模板修改后接着再繼續自動化使用進行接口測試,那樣可以做就是會比較重了, 還是根據自己需要進行相關測試策略設計。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM