0
点赞
收藏
分享

微信扫一扫

Postman的测试脚本(二)



脚本示例

  • ​​一、前言​​
  • ​​二、操作变量及发送请求脚本​​
  • ​​三、示例脚本​​
  • ​​  1.Response body:Contains string(检查响应体中是否包含一个字符串)​​
  • ​​  2.Response body:Convert XML body to a JSON Object(将XML格式的响应体转换成JSON对象)​​
  • ​​  3.Response body:Is equal to a string(检查响应体等于一个字符串)​​
  • ​​  4.Response body:JSON value check(检查响应体的JSON值)​​
  • ​​  5.Response headers:Content-Type headers check(检查响应中包含某个header)​​
  • ​​  6.Response time is less than 200ms(检查响应时间,要求小于200ms)​​
  • ​​  7.Status code:Code is 200(要求该接口响应Code为200)​​
  • ​​  8.Status code:Code name has string(要求code名称当中包含每个字符串)​​
  • ​​  9.Status code:Successful POST request(要求Status code符合某种条件)​​
  • ​​  10.Use Tiny Validator for JSON data(使用轻量级验证器)​​

一、前言

  测试脚本在发送请求,并从服务器收到响应后才开始执行。接下来看看Postman提供了那些测试脚本片段。

二、操作变量及发送请求脚本

  (1)Set a global variable(设置全局变量)的示例如下:

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

  (2)Set an environment variable(设置环境变量)的示例如下:

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

  (3)Get a global variable(获取全局变量)的示例如下:

pm.globals.get("variable_key");

  (4)Get an environment variable(获取环境变量)的示例如下:

pm.environment.get("variable_key");

  (5)Get a variable(获取变量)的示例如下:

pm.variables.get("variable_key");

  (6)Clear a global variable(清除全局变量)的示例如下:

pm.globals.unset("variable_key");

  (7)Clear an environment variable(清除环境变量)的示例如下:

pm.environment.unset("variable_key");

  (8)Send a request(发送一个请求)的示例如下:

pm.sendRequest("https://postman-echo.com/get", function (err, response) {
console.log(response.json());
});

  以上8个方法和Pre-request Script中的类似,在此不赘述,下面着重看看几个示例脚本。

三、示例脚本

  1.Response body:Contains string(检查响应体中是否包含一个字符串)

  具体示例如下:

pm.test("Body matches string", function () {
pm.expect(pm.response.text()).to.include("金庸");
});

  这里要求接口响应必须包含“金庸”字符串,否则报错,如图所示:

Postman的测试脚本(二)_xml

  测试结果为FALL,响应体中确实不包含“金庸”字样。

  2.Response body:Convert XML body to a JSON Object(将XML格式的响应体转换成JSON对象)

假如响应体是XML格式,将其后转换成JSON对象,在对其进行操作。

var jsonObject = xml2Json(responseBody);

接口示例如下:

GET请求,URL为http://www.weather.com.cn/data/sk/101010100.html(免费接口),这是一个获取天气情况的API,返回结果为XML格式,请求响应如果所示:

Postman的测试脚本(二)_字符串_02

控制台输出结果,如图所示:

Postman的测试脚本(二)_postman_03

借助上面的方法将其转换为JSON格式查看,如图所示:

Postman的测试脚本(二)_json_04

控制台输出如图所示:

Postman的测试脚本(二)_字符串_05

  3.Response body:Is equal to a string(检查响应体等于一个字符串)

具体示例如下:

pm.test("Body is correct", function () {
pm.response.to.have.body("response_body_string");
});

要求响应体必须等于某个字符串,这里,期望响应体等于“北京”,如图所示:

Postman的测试脚本(二)_json_06

其结果为FALL,应为响应体是一大段文字,不等于“北京”,如图所示:

Postman的测试脚本(二)_postman_07

  4.Response body:JSON value check(检查响应体的JSON值)

具体示例如下:

pm.test("Your test name", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.value).to.eql(100);
});

假设要求该API搜索“北京天气”是,返回值weatherinfo值等于{“city”:“北京”,“cityid”:“101010100”,“temp”:“27.9”,“WD”:“南风”,“WS”:“小于3级”,“SD”:“28%”,“AP”:“1002hPa”,“njd”:“暂无实况”,“WSE”:“❤️”,“time”:“17:55”,“sm”:“2.1”,“isRadar”:“1”,“Radar”:“JC_RADAR_AZ9010_JB”}如图所示:

Postman的测试脚本(二)_postman_08

其结果为PASS。

  5.Response headers:Content-Type headers check(检查响应中包含某个header)

具体示例如下:

pm.test("Content-Type is present", function () {
pm.response.to.have.header("Content-Type");
});

这里检查接口中含有“Content-Type”header,如图图所示:

Postman的测试脚本(二)_xml_09

检查结果为Pass,如图所示:

Postman的测试脚本(二)_测试工具_10

  6.Response time is less than 200ms(检查响应时间,要求小于200ms)

具体示例如下:

pm.test("Response time is less than 200ms", function () {
pm.expect(pm.response.responseTime).to.be.below(200);
});

这里。假设测试接口请求要求响应时间为1000ms,如图所示:

Postman的测试脚本(二)_json_11

可以看到本次请求响应时间为24ms(每次请求的时间不同),小于1000ms,测试结果为Pass,如图所示:Postman的测试脚本(二)_字符串_12

  7.Status code:Code is 200(要求该接口响应Code为200)

具体示例如下:

pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});

测试要请求接口响应code=200,如图所示:

Postman的测试脚本(二)_字符串_13

测试结果为PASS。

  8.Status code:Code name has string(要求code名称当中包含每个字符串)

具体示例如下:

pm.test("Status code name has string", function () {
pm.response.to.have.status("Created");
});

这里要求返回结果Status中包含“OK”字符串,如图所示:

Postman的测试脚本(二)_测试工具_14

测试结果为PASS。

  9.Status code:Successful POST request(要求Status code符合某种条件)

pm.test("Successful POST request", function () {
pm.expect(pm.response.code).to.be.oneOf([201, 202]);
});

假设要求响应code是200、201、202中的一个,如图所示:

Postman的测试脚本(二)_测试工具_15

响应code为200,符合条件,测试结果为PASS。

  10.Use Tiny Validator for JSON data(使用轻量级验证器)

具体示例如下:

var schema = {
"items": {
"type": "boolean"
}
};

var data1 = [true, false];
var data2 = [true, 123];

pm.test('Schema is valid', function () {
pm.expect(tv4.validate(data1, schema)).to.be.true;
pm.expect(tv4.validate(data2, schema)).to.be.true;
});

该代码片段用来检查数据类型,如天气API,响应结果中cityid、city、cityEn、country、countryEn、update_time的值都是string数据类型,data的值是列表对象object,如图所示:

Postman的测试脚本(二)_测试工具_16

下面通过Tiny Validator来设置Test,如图所示:

Postman的测试脚本(二)_字符串_17

可以看到,测试结果为PASS。

也可以换一种方式构建该检查点,如图所示:

Postman的测试脚本(二)_测试工具_18

注意:JavaScript共提供了7种数据类型,,即string(字符串)、number(数值)、boolean(布尔值)、object(对象)、undefined、null、symbol(ES6引入的一种新的原始数据类型,表示独一无二的值)。


举报

相关推荐

0 条评论