postman 常用断言语句


摘抄自: https://www.jianshu.com/p/132db486981e

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

 

在Test下添加如下代码可将返回的id值设为全局环境变量

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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM