postman常用断言总结

一,关于环境变量(Setting an environment variable )

postman.setEnvironmentVariable(“key”, “value”);
Pm.enviroment.get(“key”,”value”) //nativa app特有语法
Postman.clearEnviromentVariable(“key”);
Pm.enviroment.unset(“key”) //nativa app特有语法

二,关于全局变量(Set a global variable)

postman.setGlobalVariable(“key”, “value”);
Pm.globals.get(“key”,”value”) //nativa app特有语法
Postman.clearGlobalVaricale(“key”);
Pm.globals.unset(“key”) //nativa app特有语法

三,判断请求返回的状态为200

方法1:
pm.test(“connection Established”, function () {
pm.response.to.have.status(200);
});
方法2:
tests[“Status code is 200”] = responseCode.code == 200;

四,判断去请求返回中其他字段的值(或状态)

pm.test(“Body matches string”, function () { pm.expect(pm.response.text()).to.include(“string_you_want_to_search”);
});

五,获取json数据,并校对返回值的正确性

方法1:
var res = JSON.parse(responseBody);
tests[“rsp的值是否为操作成功”] = res.rspInf == “操作成功”;
Tests[“判断V的值是否正确”]=res.V==””
tests[“判断data的第二个元素的值(loginTime)是否正确”] = res.data.loginTime === 1548607006000;
方法2:
pm.test(“判断psCd是否=00000”, function () {
var res = pm.response.json();
pm.expect(res.rsCd).to.eql(“00000”);
});
pm.test(“判断role是否=admin”, function () {
var jsonData = pm.response.json();
pm.expect(jsonData.data.role).to.eql(“admin”);
});

六,判断响应时间

方法1:
pm.test(“Response time is less than 200ms”, function () {
pm.expect(pm.response.responseTime).to.be.below(200);
});
方法2:
tests[“Response time is less than 200ms”] = responseTime < 200;

七,判断返回数据的类型

var res = JSON.parse(responseBody); // 先获取到返回的json数据
tests[“判断loginTime的值的类型是否是number”] = typeof(res.data.loginTime) === “number”;
tests[“判断rsCd的值的类型师是否是string”] = typeof(res.data.loginTime) == “number”;
// 比较常用的集中类型:number、string、object、array、boolean、undefined

八,判断返回数据中是否存在某个元素

tests[“判断返回的元素中是否有data”] =responseBody.has(“data”);
tests[“判断返回的元素中是否有rsCd”] =responseBody.has(“rsCd”);

九,转化XML格式的响应成JSON对象

var jsonObject = xml2Json(responseBody);

十,检查JSON某字段值

var data = JSON.parse(responseBody);
tests[“Your test name”] = data.value === 100;

十一,检查Content-Type是否包含在header返回(大小写不敏感)

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

十二,检查Code name包含指定string

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

十三,检查成功post的请求(status code–Succesful POST request status code)

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

你可能感兴趣的:(postman)