Postman高级用法

Postman在Web开发时非常好用,方便管理一系列的Restful API管理

  • 基础功能
    各种Restful API请求的发送与结果分析(不介绍了)

  • 变量&环境
    变量
    这个是Postman非常常用的功能,结合环境变量可以实现快速多环境API测试
    http://{{server}}/greeting

环境
Postman高级用法_第1张图片

  • Workspace
    可以方便在不同的工作场景或者项目中切换,每个Workspace设置不同的环境,不同的请求集合Collections

  • 断言测试
    断言
    可以给每个请求设置Tests断言

pm.test("Check code", function() {
    pm.expect(pm.response.json().code).to.eql("202")
    pm.expect(pm.response.json().msg).to.eql("success")
})
//console.log特别有用,方便在写内容的时候调试,不过需要打开Console调试窗口
console.log(pm.response.json())

还可以在断言里面把返回值设置到变量中,方便其他请求使用

var jsonData = JSON.parse(responseBody);
postman.setEnvironmentVariable("token", jsonData.data.token)

更多的断言语句参考https://learning.getpostman.com/docs/postman/scripts/test_examples/

  • Pre-request Script
    可以发出请求之前,修改请求内容

  • 设置变量
    Postman高级用法_第2张图片

  • 使用变量
    Postman高级用法_第3张图片

  • 其他常用功能
    Postman Console调试窗口
    快捷键:Ctrl+Alt+C
    Postman高级用法_第4张图片

代理
在公司开发环境中,一般需要配置代理:File -> Settings -> Proxy

Mock服务器
功能挺好的,但是实际上作用并不大,开发过程中,可以使用nginx反向代理可以在本地实现mock,效率更高。

参考:https://learning.getpostman.com/docs/postman/scripts/test_examples/


//延时30秒执行这个接口
Postman高级用法_第5张图片

//延时30秒执行这个接口
setTimeout(function(){
    alert('30秒后会执行!');
},30000)

你可能感兴趣的:(Postman)