Postman与接口文档


一.接口文档

1.怎么做接口测试

发送Request的请求信息以及Response的响应信息:
1、使用浏览器的network
2、使用charles的工具
3、查看开发的接口文档

2.查看接口文档

YAPI平台:http://yapi.smart-xwork.cn/

二.Postman

1.使用Postman与接口文档做接口测试

1.1GET请求

接口文档地址:
http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?op=getMobileCodeInfo
接口文档:
GET /WebServices/MobileCodeWS.asmx/getMobileCodeInfo?mobileCode=string&userID=string 
Host: ws.webxml.com.cn
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://WebXml.com.cn/">string</string>

请求参数:

输入参数:mobileCode = 字符串(手机号码,最少前7位数字),userID = 字符串(商业用户ID) 
免费用户为空字符串;返回数据:字符串(手机号码:省份 城市 手机卡类型)。

 1.2Post请求

1.2.1请求数据为xml格式

请求文档地址
http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?op=getMobileCodeInfo
请求文档
请求方法&请求头&请求地址
POST /WebServices/MobileCodeWS.asmx HTTP/1.1
Host: ws.webxml.com.cn
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://WebXml.com.cn/getMobileCodeInfo"
请求参数
输入参数:mobileCode = 字符串(手机号码,最少前7位数字),userID = 字符串(商业用户ID) 
免费用户为空字符串;返回数据:字符串(手机号码:省份 城市 手机卡类型)。
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <getMobileCodeInfo xmlns="http://WebXml.com.cn/">
      <mobileCode>string</mobileCode>
      <userID>string</userID>
    </getMobileCodeInfo>
  </soap:Body>
</soap:Envelope>
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

  响应参数

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <getMobileCodeInfoResponse xmlns="http://WebXml.com.cn/">
      <getMobileCodeInfoResult>string</getMobileCodeInfoResult>
    </getMobileCodeInfoResponse>
  </soap:Body>
</soap:Envelope>

1.2.2请求数据为表单格式

接口文档地址
接口文档
请求方法&请求地址&请求头

POST /WebServices/MobileCodeWS.asmx/getMobileCodeInfo HTTP/1.1 Host: ws.webxml.com.cn Content-Type: application/x-www-form-urlencoded Content-Length: length
请求参数

mobileCode=string&userID=string HTTP/1.1 200 OK Content-Type: text/xml; charset=utf-8 Content-Length: length
响应参数:

<?
xml version="1.0" encoding="utf-8"?> <string xmlns="http://WebXml.com.cn/">string</string>

 2.Postman的使用

2.1Postman添加合集

 

 

2.2运行集合

2.3Postman字体大小设置

3.Postman断言

测试用例都需要加断言来判断测试的结果
在postman里面,编写断言使用的语言是:javascript,简称JS,编写的地方是在测试用例的tests区域

验证api测试用例,必须三个维度来验证,三个关系是并且的关系
    1、协议状态码
    2、业务状态码
    3、响应数据

4.Postman中关于token请求的测试

4.1token请求流程:

1.客户端使用账号密码登录到服务端
2.服务端将账号密码储存到服务端并返回一个令牌给客户端
3.客服端再有其他请求需要发送令牌给服务端

4.2动态参数(关联)的解决思路

1.首先通过登录的接口登录成功
2.通过响应数据拿到返回的授权
3.在需要授权登录的接口中定义变量(返回的授权)
4.在登录的接口中添加请求头调用变量,获取到登录接口拿到的授权
postman中调用变量名称,使用的是{{}}
5.先执行需要授权登录的接口,在执行登录接口
动态参数(关联)/上个接口的输出是下个输出的输入
1、授权登录接口输出了响应数据“授权登录令牌”
2、该“授权登录令牌”是登录接口的输入
6.必须要在集合中来执行接口,不能单独执行需要授权的接口
1、通过接口http://101.43.158.84:5000/auth,登录成功
2、登录成功后,在响应数据中返回认证授权
3、在该http://101.43.158.84:5000/auth的接口的tests中,定义变量获取access_token(授权的令牌)
4、下来在接口http://101.43.158.84:5000/index的请求中添加请求头,key为Authorization value为:jwt 获取到的授权的令牌,如Authorization:jwt {{token}}
5、下来执行的顺序必须是:
       先执行登录授权的接口http://101.43.158.84:5000/auth
       再执行http://101.43.158.84:5000/index的接口,这样就能够获取到调用变量的值
6、必须是在collection中执行,不能单独的执行http://101.43.158.84:5000/index接口,如果单独执行,依然是401,没授权

 5.Postman数据驱动

  • 概念

    那么在自动化测试中(工具&代码),把共有的数据分离出来,这个思想就是数据驱动的思想,如请求地址,那么我们可以把请求地址分离出来,
  • 解决的问题

    不管你的请求地址怎么变化,我只需要在一个地方进行维护
  • 实战

    有一个书籍管理的业务
    查看所有书籍:http://101.43.158.84:5000/v1/api/books
    添加书籍:http://101.43.158.84:5000/v1/api/books
      {
                "author": "wuya",
                "done": true,
                "name": "Python接口自动化测试实战"
        }
    查看具体的书:http://101.43.158.84:5000/v1/api/book/{{bookID}}
    删除书籍信息:http://101.43.158.84:5000/v1/api/book/{{bookID}}

    我们可以发现他们的请求地址是相同的,那么我们就可以把他们的请求地址分离出来,这样不管后期请求地址怎么变换,我们只需要再一个地方管理就可以

     断言的数据分离

6.Postman测试报告的生成

我们使用工具newman就可以生成测试报告(自动化测试的结果)
使用newman的前提是需要安装node.js,通过node.js来安装newman的工具
安装newman的命令:
npm install -g newman --registry=https://registry.npm.taobao.org

 

  • 下载安装node

    检验是否安装成功
    C:\Users\特昂糖>npm
    npm <command>
    
    Usage:
    
    npm install        install all the dependencies in your project
    npm install <foo>  add the <foo> dependency to your project
    npm test           run this project's tests
    npm run <foo>      run the script named <foo>
    npm <command> -h   quick help on <command>
    npm -l             display usage info for all commands
    npm help <term>    search for help on <term> (in a browser)
    npm help npm       more involved overview (in a browser)
    
    All commands:
    
        access, adduser, audit, bin, bugs, cache, ci, completion,
        config, dedupe, deprecate, diff, dist-tag, docs, doctor,
        edit, exec, explain, explore, find-dupes, fund, get, help,
        hook, init, install, install-ci-test, install-test, link,
        ll, login, logout, ls, org, outdated, owner, pack, ping,
        pkg, prefix, profile, prune, publish, rebuild, repo,
        restart, root, run-script, search, set, set-script,
        shrinkwrap, star, stars, start, stop, team, test, token,
        uninstall, unpublish, unstar, update, version, view, whoami
    
    Specify configs in the ini-formatted file:
        C:\Users\特昂糖\.npmrc
    or on the command line via: npm <command> --key=value
    
    More configuration info: npm help config
    Configuration fields: npm help 7 config
    
    npm@8.1.2 D:\Ruanjian\node_modules\npm
  • 安装newman

    C:\Users\特昂糖>npm install -g newman --registry=https://registry.npm.taobao.org
    npm WARN deprecated har-validator@5.1.5: this library is no longer supported
    npm WARN deprecated uuid@3.4.0: Please upgrade  to version 7 or higher.  Older versions may use Math.random() in certain circumstances, which is known to be problematic.  See https://v8.dev/blog/math-random for details.
    npm WARN deprecated uuid@3.4.0: Please upgrade  to version 7 or higher.  Older versions may use Math.random() in certain circumstances, which is known to be problematic.  See https://v8.dev/blog/math-random for details.
    npm WARN deprecated uuid@3.4.0: Please upgrade  to version 7 or higher.  Older versions may use Math.random() in certain circumstances, which is known to be problematic.  See https://v8.dev/blog/math-random for details.
    
    added 128 packages in 13s
    npm notice
    npm notice New minor version of npm available! 8.1.2 -> 8.3.0
    npm notice Changelog: https://github.com/npm/cli/releases/tag/v8.3.0
    npm notice Run npm install -g npm@8.3.0 to update!
    npm notice

    验证是否安装成功

    C:\特昂糖>newman
    Usage: newman [options] [command] Options:
    -v, --version output the version number -h, --help display help for command Commands: run [options] <collection> Initiate a Postman Collection run from a given URL or path To get available options for a command: newman <command> -h
  • 生成测试报告

    • 导出测试数据

    • 进入测试数据所在本地磁盘

      C:\Users\特昂糖>cd D:\Zhuomian
      
      C:\Users\特昂糖>d:
      
      D:\Zhuomian>dir
       驱动器 D 中的卷是 本地磁盘
       卷的序列号是 60B8-2ACA
      
       D:\Zhuomian 的目录
      
      2022/01/08  23:45    <DIR>          .
      2022/01/08  23:45    <DIR>          ..
      2022/01/05  11:50    <DIR>          a
      2022/01/05  09:39       143,171,304 charlesproxy.rar
      2022/01/07  16:35        28,020,736 node-v16.13.1-x64.msi
      2022/01/08  23:45        26,442,630 node-v16.13.1-x64.rar
      2022/01/05  14:56       116,785,528 Postman-win64-8.7.0-Setup.exe
      2021/12/21  09:15    <DIR>          python
      2022/01/07  17:06             5,443 书籍管理.postman_collection.json
      2021/10/13  22:35    <DIR>          头像
      2022/01/04  17:37    <DIR>          红包
                     5 个文件    314,425,641 字节
                     6 个目录 95,791,566,848 可用字节
    • 生成测试报告

      D:\Zhuomian>newman run 书籍管理.postman_collection.json
      newman
      
      书籍管理
      
      → 获取所有书籍
        GET http://101.43.158.84:5000/v1/api/books  [200 OK, 16.19kB, 123ms]
        √  验证协议状态码
        √  验证响应时间
        ┌
        │ {
        │   datas: [
        │     { author: 'wuya', done: true, id: 1, name
        │ m: 'Python接口自动化测试实战' },
        │     { author: '无涯', done: false, id: 2, name
        │ : 'Selenium3自动化测试实战' },
        │     { author: 'wuya', done: true, id: 3, name
        │ m: 'Python实战' },
        │     { author: 'wuya', done: true, id: 4, name
        │ m: 'Python实战' },
        │     { author: 'wuya', done: true, id: 5, name
        │ m: 'Python实战' },
        │     { author: 'wuya', done: true, id: 6, name
        │ m: 'Python实战' },
        │     { author: 'stellbars', done: true, id: 7, nam
        │ e: '乡村爱情圆舞曲' },
        │     { author: 'stellbars', done: true, id: 8, nam
        │ e: '乡村爱情圆舞曲' },
        │     { author: 'wuya', done: true, id: 9, name
        │ m: 'Python实战' },
        │     { author: 'wuya', done: true, id: 10, name
        │ 9m: 'Python实战' },
        │     { author: 'wuya', done: true, id: 11, name
        │ 9m: 'Python接口自动化测试实战' },
        │     { author: '木溪', done: true, id: 12, name
        │ : '完美事件' },
        │     { author: '无涯', done: true, id: 13, name
        │ : 'Selenium3实战' },
        │     { author: 'wuya', done: true, id: 14, name
        │ 9m: 'Python实战' },
        │     { author: '无涯', done: true, id: 15, name
        │ : 'Selenium3实战' },
        │     { author: '无涯', done: true, id: 16, name
        │ : 'Selenium3实战' },
        │     { author: '木溪', done: true, id: 17, name
        │ : '完美事件' },
        │     { author: '无涯', done: true, id: 18, name
        │ : 'Selenium3实战' },
        │     { author: 'wuya', done: true, id: 19, name
        │ 9m: 'Python接口自动化测试实战' },
        │     { author: '木溪', done: true, id: 20, name
        │ : '完美事件' },
        │     { author: 'wuya', done: true, id: 21, name
        │ 9m: 'Python实战' },
        │     { author: 'wuya', done: true, id: 22, name
        │ 9m: 'Python实战' },
        │     { author: 'wuya', done: true, id: 23, name
        │ 9m: 'Python实战' },
        │     { author: 'stellbars', done: true, id: 24, na
        │ me: '乡村爱情圆舞曲' },
        │     { author: '无涯', done: true, id: 25, name
        │ : 'Selenium3实战' },
        │     { author: 'stellbars', done: true, id: 26, na
        │ me: '乡村爱情圆舞曲' },
        │     { author: 'wuya', done: true, id: 27, name
        │ 9m: 'Python实战' },
        │     { author: '木溪', done: true, id: 28, name
        │ : '完美事件' },
        │     { author: 'wuya', done: true, id: 29, name
        │ 9m: 'Python实战' },
        │     { author: '无涯', done: true, id: 30, name
        │ : 'Selenium3实战' },
        │     { author: 'stellbars', done: true, id: 31, na
        │ me: '乡村爱情圆舞曲' },
        │     { author: 'wuya', done: true, id: 32, name
        │ 9m: 'Python实战' },
        │     { author: 'wuya', done: true, id: 33, name
        │ 9m: 'Python接口自动化测试实战' },
        │     { author: 'wuya', done: true, id: 34, name
        │ 9m: 'Python接口自动化测试实战' },
        │     { author: 'wuya', done: true, id: 35, name
        │ 9m: 'Python接口自动化测试实战' },
        │     { author: '无涯', done: true, id: 36, name
        │ : 'Selenium3实战' },
        │     { author: 'wuya', done: true, id: 37, name
        │ 9m: 'Python接口自动化测试实战' },
        │     { author: '无涯', done: true, id: 38, name
        │ : 'Selenium3实战' },
        │     { author: 'wuya', done: true, id: 39, name
        │ 9m: 'Python实战' },
        │     { author: 'wuya', done: true, id: 40, name
        │ 9m: 'Python实战' },
        │     { author: 'wuya', done: true, id: 41, name
        │ 9m: 'Python实战' },
        │     { author: 'wuya', done: true, id: 42, name
        │ 9m: 'Python接口自动化测试实战' },
        │     { author: 'wuya', done: true, id: 43, name
        │ 9m: 'Python接口自动化测试实战' },
        │     { author: 'wuya', done: true, id: 44, name
        │ 9m: 'Python接口自动化测试实战' },
        │     { author: 'wuya', done: true, id: 45, name
        │ 9m: 'Python接口自动化测试实战' },
        │     { author: '无涯', done: true, id: 46, name
        │ : 'Selenium3实战' },
        │     { author: '无涯', done: true, id: 47, name
        │ : 'Selenium3实战' },
        │     { author: '木溪', done: true, id: 48, name
        │ : '完美事件' },
        │     { author: 'wuya', done: true, id: 49, name
        │ 9m: 'Python实战' },
        │     { author: 'wuya', done: true, id: 50, name
        │ 9m: 'Python实战' },
        │     { author: 'wuya', done: true, id: 51, name
        │ 9m: 'Python接口自动化测试实战' },
        │     { author: '木溪', done: true, id: 52, name
        │ : '完美事件' },
        │     { author: 'wuya', done: true, id: 53, name
        │ 9m: 'Python实战' },
        │     { author: '无涯', done: true, id: 54, name
        │ : 'Selenium3实战' },
        │     { author: '木溪', done: true, id: 55, name
        │ : '完美事件' },
        │     { author: 'wuya', done: true, id: 56, name
        │ 9m: 'Python接口自动化测试实战' },
        │     { author: '无涯', done: true, id: 57, name
        │ : 'Selenium3实战' },
        │     { author: '无涯', done: true, id: 58, name
        │ : 'Selenium3实战' },
        │     { author: '木溪', done: true, id: 59, name
        │ : '完美事件' },
        │     { author: '木溪', done: true, id: 60, name
        │ : '完美事件' },
        │     { author: 'wuya', done: true, id: 61, name
        │ 9m: 'Python实战' },
        │     { author: 'wuya', done: true, id: 62, name
        │ 9m: 'Python实战' },
        │     { author: 'wuya', done: true, id: 63, name
        │ 9m: 'Python接口自动化测试实战' },
        │     { author: '木溪', done: true, id: 64, name
        │ : '完美事件' },
        │     { author: '无涯', done: true, id: 65, name
        │ : 'Selenium3实战' },
        │     { author: 'wuya', done: true, id: 66, name
        │ 9m: 'Python实战' },
        │     { author: '木溪', done: true, id: 67, name
        │ : '完美事件' },
        │     { author: '无涯', done: true, id: 68, name
        │ : 'Selenium3实战' },
        │     { author: 'wuya', done: true, id: 69, name
        │ 9m: 'Python实战' },
        │     { author: '无涯', done: true, id: 70, name
        │ : 'Selenium3实战' },
        │     { author: '无涯', done: true, id: 71, name
        │ : 'Selenium3实战' },
        │     { author: '木溪', done: true, id: 72, name
        │ : '完美事件' },
        │     { author: '木溪', done: true, id: 73, name
        │ : '完美事件' },
        │     { author: 'wuya', done: true, id: 74, name
        │ 9m: 'Python接口自动化测试实战' },
        │     { author: 'wuya', done: true, id: 75, name
        │ 9m: 'Python实战' },
        │     { author: '无涯', done: true, id: 76, name
        │ : 'Selenium3实战' },
        │     { author: 'wuya', done: true, id: 77, name
        │ 9m: 'Python实战' },
        │     { author: 'wuya', done: true, id: 78, name
        │ 9m: 'Python实战' },
        │     { author: 'wuya', done: true, id: 79, name
        │ 9m: 'Python接口自动化测试实战' },
        │     { author: '木溪', done: true, id: 80, name
        │ : '完美事件' },
        │     { author: 'wuya', done: true, id: 81, name
        │ 9m: 'Python接口自动化测试实战' },
        │     { author: 'wuya', done: true, id: 82, name
        │ 9m: 'Python实战' },
        │     { author: 'wuya', done: true, id: 83, name
        │ 9m: 'Python实战' },
        │     { author: 'wuya', done: true, id: 84, name
        │ 9m: 'Python实战' },
        │     { author: 'wuya', done: true, id: 85, name
        │ 9m: 'Python实战' },
        │     { author: 'wuya', done: true, id: 86, name
        │ 9m: 'Python实战' },
        │     { author: 'wuya', done: true, id: 87, name
        │ 9m: 'Python实战' },
        │     { author: 'wuya', done: true, id: 88, name
        │ 9m: 'Python实战' },
        │     { author: 'wuya', done: true, id: 89, name
        │ 9m: 'Python实战' },
        │     { author: 'wuya', done: true, id: 90, name
        │ 9m: 'Python实战' },
        │     { author: 'wuya', done: true, id: 91, name
        │ 9m: 'Python实战' },
        │     { author: 'wuya', done: true, id: 92, name
        │ 9m: 'Python实战' },
        │     { author: 'wuya', done: true, id: 93, name
        │ 9m: 'Python实战' },
        │     { author: 'wuya', done: true, id: 94, name
        │ 9m: 'Python实战' },
        │     { author: 'wuya', done: true, id: 95, name
        │ 9m: 'Python实战' },
        │     { author: 'wuya', done: true, id: 96, name
        │ 9m: 'Python实战' },
        │     { author: '无涯', done: true, id: 97, name
        │ : 'Selenium3实战' },
        │     { author: '无涯', done: true, id: 98, name
        │ : 'Selenium3实战' },
        │     { author: '无涯', done: true, id: 99, name
        │ : 'Selenium3实战' },
        │     { author: '无涯', done: true, id: 100, name
        │ m: 'Selenium3实战' },
        │     ... 30 more items
        │   ],
        │   msg: 'ok',
        │   status: 0
        │ }
        └
        √  验证status对应的值为0
        √  验证msg对应的值为ok
        √  验证第一本书籍的名称
        √  验证第二本书籍的名称
      
      → 添加书籍
        POST http://101.43.158.84:5000/v1/api/books [200 OK, 357B, 52ms]
        √  验证协议状态码
        √  验证响应时间
        ┌
        │ {
        │   status: 1002,
        │   msg: '添加书籍成功',
        │   datas: { id: 131, author: '无涯', name: 'Selenium
        │ 3实战', done: true }
        │ }
        └
        √  验证status对应的值
        √  验证msg对应的值
      
      → 查看添加的书籍
        GET http://101.43.158.84:5000/v1/api/book/131 [200 OK, 321B, 63ms]
        √  验证协议状态码
        √  验证响应时间
        √  验证status对应的值
        √  验证msg对应的值
        √  验证添加书籍的id
        √  验证添加书籍的name
      
      → 修改书籍
        PUT http://101.43.158.84:5000/v1/api/book/131 [200 OK, 376B, 55ms]
        √  验证协议状态码
        √  验证响应时间
        √  验证业务状态码
        √  验证书籍的id
        √  验证书籍的作者
      
      → 删除书籍
        DELETE http://101.43.158.84:5000/v1/api/book/131 [200 OK, 216B, 56ms]
        √  验证协议状态码
        √  验证响应时间
        ┌
        │ { msg: '删除书籍成功', status: 1009 }
        └
        √  验证status对应的值
        √  验证msg对应的值
      
      ┌─────────────────────────┬───────────────────┬───────────────────┐
      │                         │          executed │            failed │
      ├─────────────────────────┼───────────────────┼───────────────────┤
      │              iterations │                 1 │                 0 │
      ├─────────────────────────┼───────────────────┼───────────────────┤
      │                requests │                 5 │                 0 │
      ├─────────────────────────┼───────────────────┼───────────────────┤
      │            test-scripts │                10 │                 0 │
      ├─────────────────────────┼───────────────────┼───────────────────┤
      │      prerequest-scripts │                 5 │                 0 │
      ├─────────────────────────┼───────────────────┼───────────────────┤
      │              assertions │                25 │                 0 │
      ├─────────────────────────┴───────────────────┴───────────────────┤
      │ total run duration: 837ms                                       │
      ├─────────────────────────────────────────────────────────────────┤
      │ total data received: 16.73kB (approx)                           │
      ├─────────────────────────────────────────────────────────────────┤
      │ average response time: 69ms [min: 52ms, max: 123ms, s.d.: 26ms] │
      └─────────────────────────────────────────────────────────────────┘
      View Code

       

 


免责声明!

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



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