好友
阅读权限10
听众
最后登录1970-1-1
|
华夏利剑
发表于 2021-2-9 14:59
[Python] 纯文本查看 复制代码 # -* - encoding: utf -8 -* -
import json
import copy
import sys
# readme 介绍和问题如下
"""
我想根据a,b生成如下数据
生成一个新的json格式文件
json格式同postman.json格式一样
主要难点在 fromdata处理上
formdata是一个list,list中是字典
有三个元素 key values type
key = a.keys()
value for b
b.append(a.values) 把a中的value加入到b中
生成的数据需要根据 a和b的长度来定
脚本现在生成的value 是无法改变的,希望大佬可以指导一下我
"""
# - - - - - - - - - - - - -自定义接口信息 - - - - - - - - - - - - - - - -#
interFaceName = "接口测试用例"
bodyMode = "formdata"
requestMethod = "POST"
requestHost = "www.baidu.com"
requestUrl = "https://www.baidu.com"
# - - - - - - - - - - - - -自定义接口信息 - - - - - - - - - - - - - - - -#
# formdata临时变量
tmp = []
data = {}
info = {}
itemL = []
itemD = {}
protocolProfileBehavior = {}
disabledSystemHeadersD = {}
requestD = {}
headerL = []
headerD = {}
bodyD = {}
formdataL = []
urlD = {}
responseL = ["json_response"]
# - - - - - - - - - - -info - - - - - - - - - - - - - -
info["_postman_id"] = "0a88f277"
info["name"] = interFaceName
info["schema"] = "schema.getpostman.com"
# - - - - - - - - - - -header - - - - - - - - - - - - - -
headerD["key"] = "Content -Type"
headerD["value"] = "application/json; charset=UTF -8"
headerD["type"] = "text"
headerL.append(headerD)
# - - - - - - - - - - -request - - - - - - - - - - - - - -
requestD["method"] = requestMethod
requestD["header"] = headerL
requestD["body"] = bodyD
requestD["url"] = urlD
requestD["reponse"] = responseL
# - - - - - - - - - - -入参 - - - - - - - - - - - - - -
a = {"A": 1, "B": 2, "C": 3}
b = ["X", "Y", "Z"]
tmpC = []
keyL = []
valueL = []
# - - - - - - - - - - -formdata - - - - - - - - - - - - - -
for i in a.keys():
formdataD = {}
formdataD["key"] = i
keyL.append(i)
for j in b:
# formdataD = {}
c = copy.deepcopy(a)
c[i] = j
tmpC.append(c)
itemD["request"] = requestD
itemL.append(itemD)
# value在这里 key value type 其实都需要根据传参来改变的 ,type相对来说就是text
formdataD["value"] = c[i]
formdataD["type"] = "text"
formdataL.append(formdataD)
# - - - - - - - - - - -data - - - - - - - - - - - - - -
data["info"] = info
data["item"] = itemL
# - - - - - - - - - - -url - - - - - - - - - - - - - -
urlD["raw"] = requestUrl
urlD["protocol"] = "https"
urlD["host"] = requestHost
# - - - - - - - - - - -body - - - - - - - - - - - - - -
bodyD["mode"] = bodyMode
bodyD["formdata"] = formdataL
# - - - - - - - - - - -itemD - - - - - - - - - - - - - -
itemD["name"] = "form -data类型"
itemD["protocolProfileBehavior"] = disabledSystemHeadersD
disabledSystemHeadersD["accept -encoding"] = "true"
disabledSystemHeadersD["connection"] = "true"
disabledSystemHeadersD["content -type"] = "true"
disabledSystemHeadersD["user -agent"] = "true"
# - - - - - - - - - - -responseL - - - - - - - - - - - - - -
# response is python List,it's null
print(json.dumps(data, ensure_ascii=False))
|
|
发帖前要善用【论坛搜索】功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。 |
|
|
|
|