1. 程式人生 > >使用postman+newman+python做接口自動化測試

使用postman+newman+python做接口自動化測試

控制 oba ret 分享圖片 調試接口 lob sta package cli

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做接口自動化測試