关于postman的断言的解释

最近在学习postman官方文档, 顺便翻译出来,以供学习!

postman断言是JavaScript语言编写的,在postman客户端指定区域编写即可。

断言会在请求返回之后,运行,并根据断言的pass\fail情况体现在最终测试结果中。

 

clear a global variable清除全局变量pm.globals.unset("variable_key");
Clear an environment variable清除环境变量pm.environment.unset("variable_key");
get a global variable得到一个全局变量pm.globals.get("variable_key");
get a variable得到一个变量pm.variables.get("variable_key");
Get an environment variable得到一个环境变量pm.environment.get("variable_key");
response body:contains string检查response body包含字符串pm.test("Body matches string", function () {
    pm.expect(pm.response.text()).to.include("string_you_want_to_search");
});
response body:convert XML body to a JSON objectresponse body:将XML转换为JSON对象var jsonObject = xml2Json(responseBody);
response body:is equal to a string检查response body等于指定字符串pm.test("Body is correct", function () {
    pm.response.to.have.body("response_body_string");
});
response body:JSON value check检查response body中JSON某字段值pm.test("Your test name", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData.value).to.eql(100);
});
response headers:content-Type header check检查content-Type是否包含在header返回pm.test("Content-Type is present", function () {
    pm.response.to.have.header("Content-Type");
});
response time is than 200ms响应时间超过200mspm.test("Response time is less than 200ms", function () {
    pm.expect(pm.response.responseTime).to.be.below(200);
});
send s request发送一个请求pm.sendRequest("https://postman-echo.com/get", function (err, response) {
    console.log(resp  onse.json());
});
set a global variable设置一个全局变量pm.globals.set("variable_key", "variable_value");
set an environment variable设置一个环境变量pm.environment.set("variable_key", "variable_value");
status code:Code is 200状态码:代码是200pm.test("Status code is 200", function () {
    pm.response.to.have.status(200);
});
status code:code name has string状态码:代码中有指定字符串pm.test("Status code name has string", function () {
    pm.response.to.have.status("Created");
});
status code:successful POST request状态码:成功的post请求pm.test("Successful POST request", function () {
    pm.expect(pm.response.code).to.be.oneOf([201,202]);
});
use tiny validator for JSON data为json数据使用tiny验证器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;
});


  • 5
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值