美文网首页Python
批量生成postman接口

批量生成postman接口

作者: 十一岁的加重 | 来源:发表于2018-03-21 17:32 被阅读31次

    仅仅加入一个接口,无参数,无header
    后期考虑加入通用header,通用body

    
    
    # coding=utf-8
    
    import json
    import sys
    reload(sys)
    sys.setdefaultencoding('utf8')
    
    apis = 要加入postman的接口的数组
    
    outputFilePath = "/Users/用户名/Desktop/postman.json"
    itemDicts = []
    
    for api in apis:
    // 这里的base是postman里全局或者环境变量的baseUrl
        rawString = "{{base}}" + api
        paths = api.split('/')
        itemDict = { "name": api, 
                    "event": [
                                {"listen": "test",
                                "script": {
                                    "id": "14d7825c-fef1-4d4a-a7b1-bdce259f7b41",
                                    "type": "text/javascript",
                                    "exec": [
                                        "pm.test(\"接口返回 200\", function () {",
                                        "    pm.response.to.have.status(200);",
                                        "});"
                                        ]
                                        }
                                        }
                                        ],
                                        "request": {
                                            "method": "POST",
                                            "header": [],
                                            "body": {},
                                            "url": {
                                                "raw": rawString,
                                                "host": [
                                                    "{{base}}"
                                                    ],
                                                "path": paths
                                            }
                            },
                            "response": []
                            }
        itemDicts.append(itemDict)
    
    postmanDict = {
        "info": {
            "_postman_id": "bb6f7282-9c68-4b73-a463-d2087e3301c5",
            "name": "无参数",
            "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
            },
            "item":itemDicts
            }
    jsonStr = json.dumps( postmanDict, ensure_ascii=False, encoding='UTF-8')
    with open(outputFilePath, 'wt') as f:
        f.write(jsonStr)
    

    相关文章

      网友评论

        本文标题:批量生成postman接口

        本文链接:https://www.haomeiwen.com/subject/smzgqftx.html