• Postman常用技巧汇总


    最近项目因为SIT和UAT环境的服务时不时被反馈不太稳定,公司的Kibana监控也没有预警到,项目组讨论自己上一些监控功能其中有一项就是通过postman实现对接口的测试和预警,在这里刚好把postman的功能也梳理和汇总下。

    1.  postman env

    1.1 全局环境变量

    全局变量一般用来设置一些公共的变量,postman的全局变量的设置 Environments -> Globals

    全局变量的引用{{xxxx}},鼠标放在变量上显示G图标就是引用的全局变量 

    postman默认的全局变量如下表

    1.2 环境变量

    环境变量相对于全局变量来说,影响的范围更小,但是优先级比全局变量更高,一般来说的不同的Collections会对应不同Enviroment环境变量文件,环境变量的设置如下图创建一个名为postman-skill的环境变量

    引用环境变量,会发现有同样命名为{{mock-server}}的变量,环境变量优先级高于全局变量,值显示为“localhost:8980”

    2. postman Tests

    2.1  后置处理

    postman的Tests可以理解成后置处理,postman还有一个前置处理 Pre-request Script, 这两个分别表示在run test之前和之后运行的逻辑,此处讲讲Test后置处理

    2.1.1 处理返回的状态码

    pm.test("校验response code为 200", function () {
        pm.response.to.have.status(200);
    });

    2.1.2 处理返回的Response Body

    pm.test("校验response boay包含 双色球", function () {
        pm.expect(pm.response.text()).to.include("双色球");
    });
    pm.test("校验pageCount值为 1 ", function () {
        var jsonData = pm.response.json();
        pm.expect(jsonData.pageCount).to.eql(1);
    });
    pm.test("校验result数组的第一个对象的code值为 2022026 ", function () {
        var jsonData = pm.response.json();
        console.log(jsonData);
        pm.expect(jsonData.result[0].code).to.eql("2022026");
    });

    实际的Response Body

    {
        "state": 0,
        "message": "查询成功",
        "pageCount": 1,
        "countNum": 0,
        "Tflag": 0,
        "result": [
            {
                "name": "双色球",
                "code": "2022026",
                "detailsLink": "/c/2022/03/10/499007.shtml",
                "videoLink": "/c/2022/03/10/499006.shtml",
                "date": "2022-03-10(四)",
                "week": "四",
                "red": "03,04,10,15,22,24",
                "blue": "02",
                "blue2": "",
                "sales": "390646552",
                "poolmoney": "1820096321",
                "content": "内蒙古1注,江苏1注,浙江2注,福建1注,山东1注,广东2注,海南1注,深圳1注,共10注。",
                "addmoney": "",
                "addmoney2": "",
                "msg": "",
                "z2add": "",
                "m2add": "",
                "prizegrades": [
                    {
                        "type": 1,
                        "typenum": "10",
                        "typemoney": "7519808"
                    },
                    {
                        "type": 2,
                        "typenum": "129",
                        "typemoney": "244167"
                    },
                    {
                        "type": 3,
                        "typenum": "1096",
                        "typemoney": "3000"
                    },
                    {
                        "type": 4,
                        "typenum": "60656",
                        "typemoney": "200"
                    },
                    {
                        "type": 5,
                        "typenum": "1226877",
                        "typemoney": "10"
                    },
                    {
                        "type": 6,
                        "typenum": "7547686",
                        "typemoney": "5"
                    },
                    {
                        "type": 7,
                        "typenum": "",
                        "typemoney": ""
                    }
                ]
            },
            {
                "name": "双色球",
                "code": "2022025",
                "detailsLink": "/c/2022/03/08/498795.shtml",
                "videoLink": "/c/2022/03/08/498794.shtml",
                "date": "2022-03-08(二)",
                "week": "二",
                "red": "06,19,24,25,28,32",
                "blue": "04",
                "blue2": "",
                "sales": "380028752",
                "poolmoney": "1800801596",
                "content": "湖南1注,共1注。",
                "addmoney": "",
                "addmoney2": "",
                "msg": "",
                "z2add": "",
                "m2add": "",
                "prizegrades": [
                    {
                        "type": 1,
                        "typenum": "1",
                        "typemoney": "10000000"
                    },
                    {
                        "type": 2,
                        "typenum": "98",
                        "typemoney": "261907"
                    },
                    {
                        "type": 3,
                        "typenum": "1184",
                        "typemoney": "3000"
                    },
                    {
                        "type": 4,
                        "typenum": "65320",
                        "typemoney": "200"
                    },
                    {
                        "type": 5,
                        "typenum": "1271944",
                        "typemoney": "10"
                    },
                    {
                        "type": 6,
                        "typenum": "10842155",
                        "typemoney": "5"
                    },
                    {
                        "type": 7,
                        "typenum": "",
                        "typemoney": ""
                    }
                ]
            },
            {
                "name": "双色球",
                "code": "2022024",
                "detailsLink": "/c/2022/03/06/498683.shtml",
                "videoLink": "/c/2022/03/06/498682.shtml",
                "date": "2022-03-06(日)",
                "week": "日",
                "red": "07,09,10,14,19,24",
                "blue": "15",
                "blue2": "",
                "sales": "412478136",
                "poolmoney": "1733800692",
                "content": "山东1注,湖北1注,共2注。",
                "addmoney": "",
                "addmoney2": "",
                "msg": "",
                "z2add": "",
                "m2add": "",
                "prizegrades": [
                    {
                        "type": 1,
                        "typenum": "2",
                        "typemoney": "10000000"
                    },
                    {
                        "type": 2,
                        "typenum": "154",
                        "typemoney": "187504"
                    },
                    {
                        "type": 3,
                        "typenum": "1455",
                        "typemoney": "3000"
                    },
                    {
                        "type": 4,
                        "typenum": "82140",
                        "typemoney": "200"
                    },
                    {
                        "type": 5,
                        "typenum": "1599856",
                        "typemoney": "10"
                    },
                    {
                        "type": 6,
                        "typenum": "9964007",
                        "typemoney": "5"
                    },
                    {
                        "type": 7,
                        "typenum": "",
                        "typemoney": ""
                    }
                ]
            },
            {
                "name": "双色球",
                "code": "2022023",
                "detailsLink": "/c/2022/03/03/498667.shtml",
                "videoLink": "/c/2022/03/03/498666.shtml",
                "date": "2022-03-03(四)",
                "week": "四",
                "red": "07,12,17,19,24,25",
                "blue": "02",
                "blue2": "",
                "sales": "386299738",
                "poolmoney": "1667173674",
                "content": "山西1注,吉林1注,浙江1注,湖北1注,广东5注,共9注。",
                "addmoney": "",
                "addmoney2": "",
                "msg": "",
                "z2add": "",
                "m2add": "",
                "prizegrades": [
                    {
                        "type": 1,
                        "typenum": "9",
                        "typemoney": "7444170"
                    },
                    {
                        "type": 2,
                        "typenum": "98",
                        "typemoney": "280580"
                    },
                    {
                        "type": 3,
                        "typenum": "1288",
                        "typemoney": "3000"
                    },
                    {
                        "type": 4,
                        "typenum": "70720",
                        "typemoney": "200"
                    },
                    {
                        "type": 5,
                        "typenum": "1393074",
                        "typemoney": "10"
                    },
                    {
                        "type": 6,
                        "typenum": "9472092",
                        "typemoney": "5"
                    },
                    {
                        "type": 7,
                        "typenum": "",
                        "typemoney": ""
                    }
                ]
            },
            {
                "name": "双色球",
                "code": "2022022",
                "detailsLink": "/c/2022/03/01/498455.shtml",
                "videoLink": "/c/2022/03/01/498454.shtml",
                "date": "2022-03-01(二)",
                "week": "二",
                "red": "01,07,11,15,17,19",
                "blue": "06",
                "blue2": "",
                "sales": "374172850",
                "poolmoney": "1651680455",
                "content": "北京1注,河北1注,江苏3注,浙江1注,湖北1注,四川1注,宁夏1注,共9注。",
                "addmoney": "",
                "addmoney2": "",
                "msg": "",
                "z2add": "",
                "m2add": "",
                "prizegrades": [
                    {
                        "type": 1,
                        "typenum": "9",
                        "typemoney": "6751389"
                    },
                    {
                        "type": 2,
                        "typenum": "162",
                        "typemoney": "121624"
                    },
                    {
                        "type": 3,
                        "typenum": "1841",
                        "typemoney": "3000"
                    },
                    {
                        "type": 4,
                        "typenum": "95683",
                        "typemoney": "200"
                    },
                    {
                        "type": 5,
                        "typenum": "1710271",
                        "typemoney": "10"
                    },
                    {
                        "type": 6,
                        "typenum": "12553973",
                        "typemoney": "5"
                    },
                    {
                        "type": 7,
                        "typenum": "",
                        "typemoney": ""
                    }
                ]
            }
        ]
    }

    2.1.3 处理返回的Request Headers

    pm.test("校验请求头包含 X-XSS-Protection", function () {
        pm.response.to.have.header("X-XSS-Protection");
    });

    2.1.4 处理Reponse响应时间

    pm.test("校验响应时间小于 100ms", function () {
        pm.expect(pm.response.responseTime).to.be.below(100);
    });

    2.1.5 设置变量

    比如我这边test case #7 和test case #8,有关联关系,#8的请参数,需要先从#7的Response Body获取

    #7 set env

    pm.test("Status code is 200", function () {
       pm.response.to.have.status(200);
       var resJson = pm.response.json();
       console.log(resJson.token);
       if(resJson.token){
           pm.environment.set("seanTokenId", resJson.token);
       }
       console.log(pm.environment);
       console.log(pm.environment.get("seanTokenId"));
    });

    #8 get env

    以上只是一部分postman的script的处理以及断言等,但对开发人员来讲基本也够用了 

    3. postman newman

    3.1 自动化测试

    newman是postman的命令行Collection Runner,它直接从命令行运行和测试postman collection,并导出结果报表,关于怎么安装newman就不说了, 如果要生成html的报表还需要安装newman-reporter-html

    npm install -g newman-reporter-html

    运行测试用例并导出结果报表为html格式

    newman run postman-skill.postman_collection.json -e postman-skill.postman_environment.json -r cli,html --reporter-html-export newman-report/postman-skill-report.html

    如果对newman 参数不清可以看看help

    newman run -h

    选项:
    -e, --environment <path> 指定Postman环境的URL或路径
    -g, --globals <path>     指定包含Postman Globals的文件的URL或路径
    -r, --reporters [reporters] 指定用于此运行的报告器(默认值:["cli"])
    -n, --iteration-count <n>   定义要运行的迭代次数
    -d, --iteration-data <path> 指定用于迭代的数据文件(JSON 或 CSV)
    --folder <path>      指定要从集合运行的文件夹。可多次指定运行多个文件夹(默认:[])
    --global-var <value> 允许通过命令行指定全局变量,采用 key=value 格式(默认值:[])
    --env-var <value>    允许通过命令行指定环境变量,采用 key=value 格式(默认值:[])
    --export-environment <path> 完成运行后将最终环境导出到文件
    --export-globals <path>     完成运行后将最终的全局变量导出到文件
    --export-collection <path>  完成运行后将执行的集合导出到文件
    --postman-api-key <apiKey>  API Key用于从Postman API加载资源
    --bail [modifiers] 指定是否在遇到错误时优雅地停止收集运行,以及是否基于可选修饰符以错误结束运行
    --ignore-redirects 防止Newman自动跟随3XX重定向响应
    -x , --suppress-exit-code 指定是否覆盖当前运行的默认退出代码
    --silent                  阻止Newman向CLI显示输出
    --disable-unicode         强制将符合Unicode的符号替换为其纯文本等效项
    --color <value>           启用/禁用彩色输出 (auto|on|off) (默认: "auto")
    --delay-request [n]       指定请求之间的延迟程度(毫秒)(默认值:0)
    --timeout [n]             指定收集运行的超时时间(毫秒)(默认值:0)
    --timeout-request [n]     指定请求的超时时间(毫秒)(默认值:0)
    --timeout-script [n]      指定脚本的超时时间(毫秒)(默认值:0)
    --working-dir <path>      指定工作目录的路径
    --no-insecure-file-read   防止读取位于工作目录之外的文件
    -k, --insecure            禁用SSL验证
    --ssl-client-cert-list <path> 指定客户端证书配置(JSON)的路径
    --ssl-client-cert <path>      指定客户端证书 (PEM) 的路径
    --ssl-client-key <path>       指定客户端证书私钥的路径
    --ssl-client-passphrase <passphrase> 指定客户端证书密码(用于受保护的密钥)
    --ssl-extra-ca-certs <path>          指定额外受信任的CA证书 (PEM)
    --cookie-jar <path>                  指定自定义cookie jar的路径(序列化的tough-cookie JSON)
    --export-cookie-jar <path>           完成运行后将cookie jar导出到文件
    --verbose 显示收集运行和发送的每个请求的详细信息
    -h, --help 显示命令帮助

    上面已经可以通过指令法方式跑全部的测试用例,如果要进步实现自动化测试方案如下

    1. postman 自带的montiors
       - 这种方式比较快捷方便,可以实现定时跑测试用例,并自带邮件功能,缺点是有些公司是内网开发,这个功能不能使用
    2. jenkins集成newman
       - 这种方式也是方便的定制化需求,但是开发团队需要依赖devops团队的支撑,能落地的时间不确定
    3. 自己实现一个自动测试程序(js ,java)
       - 这种方式完全由开发团队自己来完成,对外部依赖较少,可以完全自己自由实现,就是需要投入开发时间

    关于postmen的全部技巧可以参考官方文档:https://learning.postman.com/docs/getting-started/introduction/ 

  • 相关阅读:
    ID:未找到命令-BASH:TTY:未找到命令
    连接/登录/访问 FTP超时、时间长,一条配置解决
    PlantUML integration plugin IDEA
    使用sc.exe delete 服务名 删除Windows下的【安装错误的、不能使用的】服务
    Eclipse JDT Icons(Java Development Tools 图标)
    Seata分布式事务——no available server to connect解决
    Slf4j Logger logger.info的使用
    SonarQube网页端登录失败的解决
    SpringBoot属性加载顺序
    W3School-SQL测验记录
  • 原文地址:https://www.cnblogs.com/hlkawa/p/15996771.html
Copyright © 2020-2023  润新知