postman 断言

1.检查请求后返回的状态码 status 为200

备注:status code:Code is 200

pm.test("Status code is 200", function () {

pm.response.to.have.status(200);

});

2.通过返回状态码检查是成功的post请求

备注:status code:successful POST request

pm.test("Successful POST request", function () {

pm.expect(pm.response.code).to.be.oneOf([201,202]);

});

3.检查response body等于指定字符串

备注:response body:is equal to a string

pm.test("Body is correct", function () {

pm.response.to.have.body("response_body_string");

});

4.检查response body中包含字符串

备注:response body:contains string

pm.test("Body matches string", function () {

pm.expect(pm.response.text()).to.include("string_you_want_to_search");

});

5.检查content-Type是否包含在header返回

备注:response headers:content-Type header check

pm.test("Content-Type is present", function () {

pm.response.to.have.header("Content-Type");

});

6.检查response body中JSON某个字段值

备注:response body:JSON value check

pm.test("Your test name", function () {

var jsonData = pm.response.json();

pm.expect(jsonData.value).to.eql(100);

});

7.检查响应时间超过200ms

备注:response time is than 200ms

pm.test("Response time is less than 200ms", function () {

pm.expect(pm.response.responseTime).to.be.below(200);

});

8.response body:将xml转换为JSON对象

备注:response body:convert XML body to a JSON object

var jsonObject = xml2Json(responseBody);

9.设置一个环境变量

备注:set an environment variable

pm.environment.set("variable_key", "variable_value");

10.设置一个全局变量

备注:set a global variable

pm.globals.set("variable_key", "variable_value");

11.清除全局变量

备注:clear a global variable

pm.globals.unset("variable_key");

12.清除环境变量

备注:Clear an environment variable

pm.environment.unset("variable_key");

13.获取一个全局变量

备注:get a global variable

pm.globals.get("variable_key");

14.获得一个环境变量

备注:Get an environment variable

pm.environment.get("variable_key");

15.获得一个变量

备注:get a variable

pm.variables.get("variable_key");

16.检查返回状态码中有指定字符串

备注:status code:code name has string

pm.test("Status code name has string", function () {

pm.response.to.have.status("Created");

});

17.发送一个请求

备注:send s request

pm.sendRequest("https://postman-echo.com/get", function (err, response) {

console.log(resp onse.json());

});

原文地址:https://www.cnblogs.com/lutong1989/p/14841217.html