postman 自動化測試 API newman 的基本使用
postman 開放了應用程序中常用的 API -- newman 供外部程序調用,newman 基於 node.js ,除了可以直接在命令行中直接運行,還支持作爲 module 在 node 代碼中調用,newman 項目地址:https://www.npmjs.com/package/newman
newman 可以直接調用 postman 導出的 collection json 文件,也可以自己手動編寫 collection json 文件,或者在 node newman 模塊中通過對象的方式設定,同時 newman 對於 collection 執行的各個時間點提供了 start、beforeItem、beforePreerquest、beforeRequest、beforeTest、done 等回調函數,可以在 node 程序中十分方便地對 collection 的執行生命週期進行干預;
# 通過 npm 下載 newman
npm install -g newman
# 直接運行 postman collection
newman run ./demo-collection.json
以下以一個簡單的示例,來演示 node 中 newman api 的使用,詳細的使用說明請參考官方文檔:https://www.npmjs.com/package/newman
測試場景描述
假設我需要對接口 http://localhost:8080/project/getHolidayByTimes.do 接口進行測試,該接口返回請求參數 startTime,stopTime 之間的假期時間,響應的 json 類似如下:
我的測試數據儲存在本地數據庫 postman_test 的數據表 holiday_case 中,如下:
示例代碼
代碼結構:
我的 collection 已經事先在 postman 中編輯好導出了,collection 只包含一個 item,item 中包含斷言腳本;
getHolidayByTimes-test-collection.json
{
"info": {
"_postman_id": "fd24328a-4dd3-4d65-985f-40cb44c44341",
"name": "getHolidayByTimes-test",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
},
"item": [{
"_postman_id": "eee4e430-0b0e-47de-8574-a4d763202ff9",
"name": "getHolidayByTimes.do",
"event": [{
"listen": "test",
"script": {
"id": "0d80d11b-7920-47b6-88b8-6770c38bd060",
"type": "text/javascript",
"exec": [
"var jsonData = JSON.parse(responseBody)",
"",
"var result = jsonData.code == data.code ;",
"if(data.code == \"0\"){",
" result = result && (jsonData.data.holidays == data.assertTime)",
"}",
"",
"tests[data.comment] = result; ",
"",
""
]
}
}],
"request": {
"method": "POST",
"body": {
"mode": "urlencoded",
"urlencoded": [{
"key": "startTime",
"value": "{{startTime}}",
"type": "text"
},
{
"key": "stopTime",
"value": "{{stopTime}}",
"type": "text"
}
]
},
"url": "{{host}}/getHolidayByTimes.do"
},
"response": []
}]
}
node 腳本代碼:
testGetHolidayByTimes.js
/**
* 測試 newman API
* @author yulinying 2018/8/2
* @description 測試 wxqyh-ask-PortalAskCtl # getHolidayByTimes 接口,從本地數據庫中讀取測試數據;
*/
var newman = require("newman");
var mysql = require("mysql");
//從本地數據庫中獲取測試數據
var connection = mysql.createConnection({
host: "localhost",
user: "root",
password: "mysql1994assad",
database: "postman_test"
});
connection.connect();
connection.query("select startTime,stopTime,code,assertTime,comment from holiday_case", function(error, results) {
if (error) throw error;
var data = convertResultToData(results);
//運行 collectin 測試
newman.run({
collection: require("./getHolidayByTimes-test-collection.json"),
reporters: ["cli", "html"],
iterationData: data,
environment: {
"values": [{
"key": "host",
"value": "http://localhost:8080/holyshit",
"type": "text",
"enabled": true
}]
}
});
});
connection.end();
//轉化 node-mysql results 爲 postman collection 迭代數據格式
function convertResultToData(results) {
var data = new Array();
for (i = 0; i < results.length; i++) {
var row = results[i];
var dataRow = new Object();
dataRow.startTime = row.startTime;
dataRow.stopTime = row.stopTime;
dataRow.code = row.code;
dataRow.assertTime = row.assertTime;
dataRow.comment = row.comment;
data.push(dataRow);
}
return data;
}
運行結果
我在代碼中設置了 newman 的報告方式爲控制檯和html,他們的結果分別如下,可以看到,newman 的測試報告方式還是十分舒服的,已經很接近 postman 的 Runner 測試界面,可以十分清楚看到測試用例通過和不通過的數量報告;
控制檯:
html 頁面: