Postman接口测试_添加断言
1、设置环境变量
postman.setEnvironmentVariable("variable_key", "variable_value");
postman.clearEnvironmentVariable("variable_key"); ——清除环境变量
2、设置全局变量
postman.setGlobalVariable("variable_key", "variable_value");
postman.clearGlobalVariable("variable_key"); ——清除全局变量
3、检查response的body中是否包含字符串
tests["Body matches string"] = responseBody.has("string_you_want_to_search");
4、将XML的响应体转换成JSON对象
var jsonObject = xml2Json(responseBody);
5、检查response的body是否等于一个字符串
tests["Body is correct"] = responseBody === "response_body_string";
6、检查json值
var jsonData = JSON.parse(responseBody);
tests["Your test name"] = jsonData.value === 100;
7、检查响应头内容类型是否存在
tests["Content-Type is present"] = postman.getResponseHeader("Content-Type");
8、检查响应时间是否小于200秒
tests["Response time is less than 200ms"] = responseTime < 200;
9、检查响应状态码是否为200
tests["Status code is 200"] = responseCode.code === 200;
10、检查响应状态码名字是否包含一个字符串
tests["Status code name has string"] = responseCode.name.has("Created");
11、通过设置响应状态码来检查post请求是否成功
tests["Successful POST request"] = responseCode.code === 200 || responseCode.code === 201;
12、Use TinyValidator for JSON data
tests["Successful POST request"] = responseCode.code === 200 || responseCode.code === 202;
var schema = {
"items": {
"type": "boolean"
}
};
var data1 = [true, false];
var data2 = [true, 123];
tests["Valid Data1"] = tv4.validate(data1, schema);
tests["Valid Data2"] = tv4.validate(data2, schema);
console.log("Validation failed: ", tv4.error);