post_04_assert_请求参数预处理_pre_requests

postman_09

当一个接口发送请求有返回结果后,如何知道返回的结果符合预期?可以在 postman 里面的 Tests 写脚本断言符合结果符合预期。
Tests 是接口返回 response 之后的脚本操作,可以使用 JavaScript 为 Postman API 请求编写 Tests 脚本

Tests编写

Tests 可以添加到单个请求,文件夹和集合

Tests里面的信息类似后置,如参数传递

https://www.cnblogs.com/yoyoketang/p/14746882.html

 

pm.test("response must be valid and have a body", function () { pm.response.to.be.ok; pm.response.to.be.withBody; pm.response.to.be.json; });

// 校验code为0 pm.test("response code must to be 0", function () { pm.expect(pm.response.json().code).to.equal(0); }); //校验msg 为 login success!

pm.test("response msg must to be login success!", function () { pm.expect(pm.response.json().msg).to.equal("login success!"); }); //校验token 长度为40位

pm.test("response token length must to be 40", function () { pm.expect(pm.response.json().token).to.lengthOf(40); });

校验提示

 

 

 

09点击code 生成不同语言的请求

 

 

 

postman使用教程10-请求前参数预处理(pre-request)

 
// 获取时间戳 var timestrip = Math.round(new Date()/1000).toString() console.log(timestrip); // 设置环境变量env_username的值
pm.environment.set("env_username", 'test_'+Math.round(new Date()/1000));  //可以先在 pre-request中设置变量然后在body中请求时再调用;和tests中的设置参数传递类似

Pre-request Script 中常用代码

pm.globals.unset("variable_key"); 清除全局变量
pm.environment.unset("variable_key");  清除环境变量
pm.globals.get("variable_key");      获取全局变量
pm.variables.get("variable_key");    获取一个变量
pm.environment.get("variable_key");      获取环境变量
pm.sendRequest("https://postman-echo.com/get", function (err, response) {
    console.log(response.json());
});  发送一个请求
pm.globals.set("variable_key", "variable_value");  设置环境变量
 
https://www.cnblogs.com/yoyoketang/p/14750649.html  postman做签名,没用过,直接用python写就可以
 

预处理(pre-request) 发送请求

pre-request 发送请求

点 Send a request 快速生成一个请求示例

  • pm.sendRequest 是发送一个请求
  • function中的err表示请求返回的错误信息,
  • response表示响应内容
  • console.log()是控制台输出日志
  •  

     

发送一个post请求示例

// Example with a full-fledged request
const postRequest = {
  url: 'https://postman-echo.com/post',
  method: 'POST',
  header: {
    'Content-Type': 'application/json',
    'X-Foo': 'bar'
  },
  body: {
    mode: 'raw',
    raw: JSON.stringify({ key: 'this is json' })
  }
};
pm.sendRequest(postRequest, (error, response) => {
  console.log(error ? error : response.json());
});

参数说明:

  • const是js中用来定义变量的关键字,由const定义的变量不可以修改,而且必须初始化
  • url表示要发送的请求url地址;
  • method指定请求方法 GET/POST;
  • header定制请求头信息,传json格式的数据的话,需定义请求头为Content-Type:application/json
  • body 表示post请求body参数
  • JSON.stringify() 方法是将一个JavaScript值(对象或者数组)转换为一个JSON字符串

 

posted @ 2021-07-18 16:52  walkerpython  阅读(99)  评论(0编辑  收藏  举报