华夏利剑 发表于 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 forb
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 = j
      tmpC.append(c)
      itemD["request"] = requestD
      itemL.append(itemD)
      # value在这里 key value type 其实都需要根据传参来改变的 ,type相对来说就是text
      formdataD["value"] = c
      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))

518 发表于 2021-2-9 19:00

直接修改data可以不
比如
```
data['item']['request']['body']['formdata']['value']
```

学着用 发表于 2021-2-13 20:52

下载看看。
页: [1]
查看完整版本: [python] 生成接口测试用例 半成品