postman之接口关联、断言
一、自动关联接口
1、引用json
//接口响应
console.log(pm.response.json())
// 获取token
let token=pm.response.json.result_token
console.log(token)
//自动保存到全局变量
pm.g1oba1s.set('token',token)
2、正则表达式
//接口响应
console.log(responseBody)
//获取接口
let token = responseBody.match('"result_token":"(.*?)"')
console.log(token)
//自动保存到全局变量
pm.globals.get('token',token[1]);
二、断言示例
//断言状态码
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
//断言响应正文包含指定字符
pm.test("Body matches string", function () {
pm.expect(pm.response.text()).to.include("string_you_want_to_search");
});
//断言状态文本
pm.test("Status code name has string", function () {
pm.response.to.have.status("Created");
});
//断言响应正文完全相等
pm.test("Body is correct", function () {
pm.response.to.have.body("response_body_string");
});
//断言一个post请求返回的状态码是不是在指定的范围内
pm.test("Successful POST request", function () {
pm.expect(pm.response.code).to.be.oneOf([201, 202]);
});
//断言响应时长
pm.test("Response time is less than 200ms", function () {
pm.expect(pm.response.responseTime).to.be.below(200);
});
//断言json值 == 100
pm.test("Your test name", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.value).to.eql(100);
});
//断言响应头
pm.test("Content-Type is present", function () {
pm.response.to.have.header("Content-Type");
});