接口测试:postman之断言的使用

postman断言作用及怎么使用

1.环境变量

(1)设置环境变量:

postman.setEnvironmentVariable("key", "value");

(2)获取环境变量:

postman.getEnvironmentVariable("key");
获取一个环境变量(其值是一个字符串对象)

(3)清楚一个环境变量

postman.clearEnvironmentVariable("key");

2.全局变量

(1)设置全局变量:

postman.setGlobalVariable("key", "value");

(2)获取全局变量:

postman.getGlobalVariable("key");
(3)清楚全局变量:

postman.clearGlobalVariable("key");

3.检查响应中包含string

tests["Body matches string"] = responseBody.has("string_you_want_to_search");

4.转化XML格式的响应成JSON对象

var jsonObject = xml2Json(responseBody);

5.检查response是否等于一个字符串

 tests["Body is correct"] = responseBody === "response_body_string";

6.检查JSON某字段值

var data = JSON.parse(responseBody);

tests["Your test name"] = data.value === 100;

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

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

//Note: the getResponseHeader() method returns the header value, if it exists.

8.检查Content-Type是否包含在header返回(大小写敏感)

 tests["Content-Type is present"] = responseHeaders.hasOwnProperty("Content-Type");

9.响应时间检查

(1)检查请求耗时时间小于200ms

tests["Response time is less than 200ms"] = responseTime < 200;

(2)响应时间在一个特定的范围内(包括下限和上限)

tests["Response time is acceptable"] = _.inRange(responseTime, 100, 1001);

10.检查Status code为200或者301

tests["Status code is 200"] = responseCode.code === 200 || responseCode.code === 301;

11.检查Code name是否包含字符串:

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

12.检查成功post的请求status code

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

13. 将TinyValidator用于JSON数据
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);

14. 解码base64编码数据
var intermediate,
base64Content, // assume this has a base64 encoded value
rawContent = base64Content.slice('data:application/octet-stream;base64,'.length);

intermediate = CryptoJS.enc.Base64.parse(base64content); // CryptoJS is an inbuilt object, documented here: https://www.npmjs.com/package/crypto-js
tests["Contents are valid"] = CryptoJS.enc.Utf8.stringify(intermediate); // a check for non-emptiness

pm.response.to.be.*

The properties inside the pm.response.to.be object allows you to easily assert a set of pre-defined rules.

  • pm.response.to.be.info

    Checks 1XX status code

  • pm.response.to.be.success

    Checks 2XX status code

  • pm.response.to.be.redirection

    Checks 3XX status code

  • pm.response.to.be.clientError

    Checks 4XX status code

  • pm.response.to.be.serverError

    Checks 5XX

  • pm.response.to.be.error

    Checks 4XX or 5XX

  • pm.response.to.be.ok

    Status code must be 200

  • pm.response.to.be.accepted

    Status code must be 202

  • pm.response.to.be.badRequest

    Status code must be 400

  • pm.response.to.be.unauthorized

    Status code must be 401

  • pm.response.to.be.forbidden

    Status code 403

  • pm.response.to.be.notFound

    Status code of response is checked to be 404

  • pm.response.to.be.rateLimited

    Checks whether response status code is 429

发布了52 篇原创文章 · 获赞 13 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/yijinaqingan/article/details/88628071
今日推荐