美文网首页
postman使用教程:常用断言语句分析与使用举例

postman使用教程:常用断言语句分析与使用举例

作者: 闲酿星河 | 来源:发表于2018-12-14 21:04 被阅读50次
1. postman的断言解释

http://www.51testing.com/html/01/n-3724901.html 对postman内置断言的说明。

postman
2. 断言语句使用举例
  • 发送一个get请求

https://www.v2ex.com/api/topics/hot.json
Method: GET
Authentication: None

postman接口请求
  • 验证返回结果
  1. 判断请求的返回状态码为200(200表示请求正常)
pm.test("判断返回状态为200", function (){
pm.response.to.have.status(200);
});

2.判断返回元素中是否有node

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

3.判断返回的的第一篇root的值为false

pm.test("判断root为false", function () {
var jsonData = pm.response.json();
pm.expect(jsonData[0].node.root).to.eql(false);});
断言结果
  • 使用postman 控制台进行输出
    打开postman console(点击“View->Show Postman Console”)
console.log(jsonData.length)
image.png
  • 设置环境变量
    将返回的id值设为当前环境变量
pm.environment.set("id", jsonData[0].node.id);
image.png

将返回的id值设为全局环境变量

pm.globals.set("global_id", jsonData[0].node.id);
image.png

相关文章

网友评论

      本文标题:postman使用教程:常用断言语句分析与使用举例

      本文链接:https://www.haomeiwen.com/subject/dqrnhqtx.html