API开发神器-Postman

Postman helps you develop APIs faster.

豪华午餐

Postman

Postman

构建、管理、文档化API

  • 在线安装
  • Chrome插件版
    https://chrome.google.com/webstore/detail/postman/fhbjgbiflinjbdggehcddcbncdddomop
  • MAC版
    https://www.getpostman.com/app/postman-osx
  • 离线安装
    使用已经安装好的Postman文件夹,机器默认存放目录:/Library/Application\ Support/Google/Chrome/Default/Extensions
    进入chrome://extensions/,选择Load unpacked extensions,加载Postman的文件夹
API开发神器-Postman_第1张图片
Newman(我真的不是MP3)

Newman

Newman is a command-line collection runner forPostman. It allows you to effortlessly run and test a Postman collection directly from the command-line. It is built with extensibility in mind so that you can easily integrate it with your continuous integration servers and build systems.

  • 安装: newman:npm install -g newman
  • 功能: 直接运行Postman的接口Collection数据(本地或远程)
  • 运行: 本地Postman保存的Collection内容
➜  Downloads newman -c Demo.postman_collection.json

Iteration 1 of 1
200 1479ms Open the url of Baidu. [GET] http://www.baidu.com
    ✔ Status code is 200

Summary:

Parent                      Pass Count   FailCount
-------------------------------------------------------------
Collection Demo                      1           0

Total                                1           0
  • 运行远程Postman
➜  ~ newman -u https://www.getpostman.com/collections/676a42223e14bf54ad84

Iteration 1 of 1
200 240ms Open the url of Baidu. [GET] http://www.baidu.com
    ✔ Status code is 200

Summary:

Parent                      Pass Count   FailCount
-------------------------------------------------------------
Collection Demo                      1           0

Total                                1           0
API开发神器-Postman_第2张图片
Interceptor

Interceptor

Postman interceptor brings the power of your Chrome window to Postman! You can set custom headers (including cookies) from within Postman, and view cookies already set on the domain. You can also capture requests being sent from Chrome and import them into Postman. This makes building APIs a breeze!

  • 安装
    https://chrome.google.com/webstore/detail/postman-interceptor/aicmkgpgakddgnaphhhpliifpcfhicfo/
  • 功能
  • 记录浏览器请求并直接导入到Postman
  • 可添加Filter,对浏览器中的请求进行过滤

初级使用

录制API-Interceptor

  • 设置浏览器中的Interceptor:打开开关、设置Filter(仅过滤www.jianshu.com
    API开发神器-Postman_第3张图片
    Chrome中设置Interceptor
  • 设置Postman中的Interceptor:打开开关
    Postmsn中打开Interceptor
  • 开始录制:在Chrome浏览器中输入http://www.jianshu.com/p/4a677c5f79f1,回车后,查看Postman的请求录入
    API开发神器-Postman_第4张图片
    录制API结果

回放API-Postman

  • 选择Postman中的API:Get http://www.jianshu.com/p/4a677c5f79f1
    API开发神器-Postman_第5张图片
    指定API
  • 点击Send,进行回放。可查看到请求的返回状态码200、数据Body
    API开发神器-Postman_第6张图片
    回放结果

指定API请求-Postman

  • URL输入框中,输入目标地址:http://www.jianshu.com/p/4a677c5f79f1。点击Send按钮
    API开发神器-Postman_第7张图片
    1步搞定指定API请求

高级使用

规划管理API - Postman+Collection

将所有的API进行分类管理,如按模块、系统、类型

保存API至Collection

  • 选中需要保存的API,点击请求列表中的Save to collection
    API开发神器-Postman_第8张图片
    Save to collection
  • 可将此API保存至已有的Collection新增Collection

重命令API请求-便于管理和查看

  • 切换到Collections列表,点击API请求的操作区,选择Edit
    API开发神器-Postman_第9张图片
    Edit-API信息

常用权限应用尽有-Authorization

API开发神器-Postman_第10张图片
Authorization设置
  • Postman在请求时,可指定此次请求Auth方式
  • 可设置Auth中的具体内容,完全自行定义。如设置Basic Auth中的用户名和密码
API开发神器-Postman_第11张图片
Basic Auth

完全自定义的头-header

API开发神器-Postman_第12张图片
Header设置
  • 编辑已有Header中的元素
  • 新增Header中请求时,需要的Key:Value
  • 禁用及启用Header中的元素,对于不清楚API请求时,具体哪些是必须要传递的比较有用

完美的数据体-body

API开发神器-Postman_第13张图片
Body设置
  • 在请求需要发送数据体时,可通过设置body内容
  • 支持form-data/x-www-urlencoded/raw/binary,格式的数据

请求前还能做点事-Pre-request Script

API开发神器-Postman_第14张图片
Pre-request Script设置
  • 请求发送前,可进行一些脚本设置。如:设置或清除参数、变量

验证API请求结果-Tests

Postman提供了常用的测试功能:返回内容处理、状态码判断、请求超时等。点击后自动添加到Test脚本中

API开发神器-Postman_第15张图片
Test命令集合

进阶使用

带你快速走向各个环境-Environment

  • 添加QA环境URL地址变量
    API开发神器-Postman_第16张图片
    添加QA环境的地址变量
  • 添加DEV环境的URL地址变量。此时就有两个环境地址
    API开发神器-Postman_第17张图片
    QA、DEV,两个环境
  • 修改请求中的URL地址为:{{URL}}p/4a677c5f79f1,切换环境至QA环境
    变更URL、切换环境
  • 点击Send,请求QA环境中的数据
    API开发神器-Postman_第18张图片
    真实的请求QA环境

使用Evnironment,可设置环境地址及环境中的不同数据,便于在跨环境后, 相同API可快速使用,减少调整API的成本

分享你的成果-导出/导入 or share

导出-Download

  • Postman中有Download功能(即导出功能),且Download后的可直接导入
  • 可将CollectionEnvironment的数据进行Download
    PS:导出的数据其实是JSON数据格式,可随意

导入-Import

  • 导入所有导出的数据
  • 被导入的数据格式与内容与导出时,完全一致

分享-share

  • share功能需要登录后,才能使用。且shared成功后,对应的链接会保存到用户数据中
    API开发神器-Postman_第19张图片
    share-link
  • 点击链接,会直接打开share的API设置内容

导出/导入、share,便于在团队内部协作时使用,API的请求及管理团队化

海量执行你的请求 - Runner

当你有大量的API时,肯定想一次执行多个多次执行多个,Postman的Runner可以满足你的需求。

  • 调整API的结构、添加必要的测试验证
    API开发神器-Postman_第20张图片
    调整
  • 打开Runner,设置执行参数:选择要执行的Collection、执行多少次Interation、请求的延迟Delay、数据文件、变量控件
    API开发神器-Postman_第21张图片
    执行参数
  • Start Test查看运行结果RESULTS
    API开发神器-Postman_第22张图片
    Results

CI挂起来 - Newman

直接使用Newman命令,快速把Postman与CI集成起来(直接使用shell命令,即可)。

newman -c demo.postman_collection --exitCode 1

PS:

  • 3.2.0版本之后,将原来的Jetpack(Runner/Newman)功能免费,以上所提到的功能全部免费使用
  • 本文使用版本为4.2.2

参考

  • Postman官网
    http://getpostman.com/
  • Newman
    https://www.npmjs.com/package/newman
  • Postman Collection
    http://www.getpostman.com/docs/collections
  • Postman Environment
    https://www.getpostman.com/docs/test_multi_environments
  • How to write powerful automated API tests with Postman, Newman and Jenkins
    http://blog.getpostman.com/2015/09/03/how-to-write-powerful-automated-api-tests-with-postman-newman-and-jenkins/

你可能感兴趣的:(API开发神器-Postman)