postman做接口自動化測試
postman是一款API調試工具,可用於測試接口,相類似的工具還有jmeter、soupUI。通過postman+newman+python可以批量運行調試接口,達到自動化測試的效果。
1、PostMan安裝
共有兩種方式,一種是chrome瀏覽器上的插件,一種是postman客戶端。我使用的是postman客戶端。
1)在Chrome瀏覽器怎么安裝Postman
https://www.cnblogs.com/mafly/p/postman.html
2)安裝Postman客戶端
a、下載軟件https://www.getpostman.com/apps
b、安裝
2、使用
1)發送請求、查看響應
2)環境變量、全局變量
環境變量:只作用於設置的環境
設置環境變量:pm.environment.set("variable_key", "variable_value");
獲取環境變量:pm.environment.get("variable_key");
全局變量:作用於所有環境
設置全局變量:pm.globals.set("variable_key", "variable_value");
獲取全局變量:pm.globals.get("variable_key");
使用例子:
var data=JSON.parse(responseBody);
var act=data.data.accessToken;
postman.setGlobalVariable("accessToken", act);
postman.setGlobalVariable("userId", data.data.userId);
postman.setGlobalVariable("refreshToken", data.data.refreshToken);
var afterUrl="?access_token="+act+"&userId="+data.data.userId;
pm.globals.set("afterUrl", afterUrl);
console.log(afterUrl)
使用變量:
在使用的變量地方用 {{variableName}}代替
具體查看文檔:https://www.getpostman.com/docs/postman/environments_and_globals/variables
3)設置斷言
tests["Your test nickName"] = data.data.nickName === "2589" //響應內容 nickName =2589
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
}); //返回為200
var responseJSON=JSON.parse(responseBody);
tests['response matches the data posted'] = (responseJSON.data && responseJSON.data.length === 10);
//返回data數據共10條
4)調試console
需要在postman客戶端,點擊 view->show postman console 調出
在test或 Pre-request Script中寫腳本打印出有疑問的值
console.log(variableName); 之后運行請求
5)collection
需要在postman客戶端,點擊collection->Runner ,運行
具體查看文檔:https://www.getpostman.com/docs/postman/collection_runs/starting_a_collection_run
6)具體使用如下圖
\
7)導出json文件
2、newman安裝
官方幫助文檔地址:https://www.npmjs.com/package/newman
1)需要安裝nodejs,並配置好環境
2)打開控制台,運行:npm install -g newman
3)校驗是否安裝成功,運行:newman --version
Newman 執行腳本
Newman在3版本后做了比較大的改動,但是運行命令越來越簡單如下:
newman run <collection-file-source> [options]
run 后面跟上要執行的json文件或者URL(json 和 URL 都由postman導出生成),再后面跟一些參數,例如環境變量,測試報告,接口請求超時時間等等。最后給兩個完整的例子做參考:
newman run D:/Buddy_Product_Enviroment.postman_collection.json --reporters cli,html,json,junit --reporter-json-export D:/jsonOut.json --reporter-junit-export D:/xmlOut.xml --reporter-html-export D:/htmlOut.html
3、使用python腳本執行newman
# coding=utf-8
import time
import os
class postmanApiTest:
#運行postman生成報告
#通過newman
def postman(self):
jSONfname = 'D:/htmlOut' + time.strftime('%Y-%m-%d', time.gmtime())+'.html'
# cmd = 'newman run D:/Buddy_Test_Enviroment.postman_collection.json --reporters cli,html,json,junit --reporter-html-export '+jSONfname
cmd='newman run D:/Buddy_Product_Enviroment.postman_collection.json --reporters cli,html,json,junit --reporter-json-export D:/jsonOut.json --reporter-junit-export D:/xmlOut.xml --reporter-html-export D:/htmlOut.html'
os.system(cmd)
print('------------------------------------------------------------')
print(jSONfname)
if os.path.isfile(jSONfname):
return jSONfname
print(jSONfname)
else:
return False
if __name__ == '__main__':
a=postmanApiTest()
a.postman()
4、最終生成報告如下:
使用postman+newman+python做接口自動化測試
接口是用來連接服務端和客戶端,一般返回的數據都是json。
get和post請求的區別:
1. get請求比post請求安全
2. get請求參數有長度限制,post請求沒有
3. get請求沒有body,參數都是放在url里面,而post請求是放在body里面的。
http請求狀態碼:
1. 200 #2 代表請求成功
2. 404 #4 代碼客戶端發出去的請求有問題
3. 300 #3 代表重定向
4. 500、502 #5代表服務端有問題
postman如何定義變量
打開postman,點擊右上角的設置-Global,設置變量的名稱及內容。使用時直接用{{變量名}}引用即可
postman如何做接口自動化
1、准備接口腳本
2、准備測試數據
第一行寫參數名稱,接口中用{{參數名}}引用。
3、根據測試數據准備校驗結果(每一條測試數據對應不同的結果,若想逐條校驗同樣需將要校驗的數據寫在text文件中)
所有的檢查點都寫在test中。右側有很多檢查的內容,可根據需要選擇
其中「Response body:Contains string」是檢查結果包含的內容(紅色字體為可替換的變量)
pm.test("Body matches string", function () {
pm.expect(pm.response.text()).to.include("檢查的內容");
});
當每一條數據的檢查內容都不一樣時,就需要用到「Set a global variable」作為變量來替換檢查的內容
pm.globals.set("變量名");
最終應為:
pm.test("Body matches string", function () {
pm.expect(pm.response.text()).to.include("pm.globals.set("變量名")");
});
4、腳本及數據准備完畢后,點擊postman左上角的「Runner」,選擇要測試的腳本,設置好數據后完畢后點擊Run,將會自動執行測試,執行完成后會返回測試結果。