Postman常用方法

编辑器旁边列出常用的代码段来辅助写tests中的脚本,选择要添加的代码段,并将相应的代码添加到测试编辑器中:

 

1. 清除一个全局变量

     Clear a global variable

    对应脚本:

    postman.clearGlobalVariable("variable_key");

    参数:需要清除的变量的key

 

2.清除一个环境变量

    Clear an environment variable

    对应脚本:

    postman.clearEnvironmentVariable("variable_key");

    参数:需要清除的环境变量的key

 

3.response包含内容

    Response body:Contains string

    对应脚本:

    tests["Body matches string"] =responseBody.has("string_you_want_to_search");

    参数:预期内容

 

4.将xml格式的response转换成son格式

    Response body:Convert XML body to a JSON Object

    对应脚本:

    var jsonObject = xml2Json(responseBody);

    参数:(默认不需要设置参数,为接口的response)需要转换的xml

 

5.response等于预期内容

    Response body:Is equal to a string

    对应脚本:

    tests["Body is correct"] = responseBody === "response_body_string";

    参数:预期response

 

6.json解析key的值进行校验

    Response body:JSON value check

    对应脚本:

    tests["Args key contains argument passed as url parameter"] = 'test' in responseJSON.args

    参数:test替换被测的值,args替换被测的key

 

7.检查response的header信息是否有被测字段

    Response headers:Content-Type header check

    对应脚本:

    tests["Content-Type is present"] = postman.getResponseHeader("Content-Type");

    参数:预期header

 

8.响应时间判断

    Response time is less than 200ms

    对应脚本:

    tests["Response time is less than 200ms"] = responseTime < 200;

    参数:响应时间

 

9.设置全局变量

      Set an global variable

      对应脚本:

      postman.setGlobalVariable("variable_key", "variable_value");

      参数:全局变量的键值

 

10.设置环境变量

      Set an environment variable

      对应脚本:

      postman.setEnvironmentVariable("variable_key", "variable_value");

      参数:环境变量的键值

 

11.判断状态码

      Status code:Code is 200

      对应脚本:

      tests["Status code is 200"] = responseCode.code != 400;

      参数:状态码

 

12.检查code name 是否包含内容

      Status code:Code name has string

      对应脚本:

      tests["Status code name has string"] = responseCode.name.has("Created");

      参数:预期code name包含字符串

 

13.成功的post请求

      Status code:Successful POST request

      对应脚本:

      tests["Successful POST request"] = responseCode.code === 201 || responseCode.code === 202;

 

14.微小验证器

       Use Tiny Validator for JSON data            

       对应脚本: 

        var schema = {

         "items": {

         "type": "boolean"

             }

         };

        var data1 = [true, false];

        var data2 = [true, 123];

        console.log(tv4.error);

        tests["Valid Data1"] = tv4.validate(data1, schema);

        tests["Valid Data2"] = tv4.validate(data2, schema);

        参数:可以修改items里面的键值对来对应验证json的参数

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在使用Postman时,有一些常用的函数可以帮助我们进行接口测试和参数处理。 1. postman.setNextRequest("")函数可以用来控制接口的流程。可以通过这个函数指定下一个要执行的接口名称,从而实现接口之间的跳转。例如,postman.setNextRequest("接口名称")可以跳转到指定的接口,而postman.setNextRequest("null")可以中断接口的执行。 2. postman可以自动解析出对应的参数。当我们发送请求时,Postman会自动解析出接口中所需的参数,并将其添加到请求中。可以方便地通过勾选或不勾选的方式来选择是否传递某个参数。 这些常用函数可以帮助我们在使用Postman进行接口测试时更加方便地控制接口的执行流程,并自动解析参数。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [postman使用教程](https://blog.csdn.net/LAM1006_csdn/article/details/120614000)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [4、postman常用函数](https://blog.csdn.net/LetsStudy/article/details/98982140)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值