一.接口文檔
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] │ └─────────────────────────────────────────────────────────────────┘
-