日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

      歡迎訪問 生活随笔!

      生活随笔

      當前位置: 首頁 > 编程语言 > python >内容正文

      python

      python做自动化控制postman_使用postman+newman+python做接口自动化测试

      發布時間:2023/12/1 python 37 豆豆
      生活随笔 收集整理的這篇文章主要介紹了 python做自动化控制postman_使用postman+newman+python做接口自动化测试 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

      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 [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、最終生成報告如下:

      總結

      以上是生活随笔為你收集整理的python做自动化控制postman_使用postman+newman+python做接口自动化测试的全部內容,希望文章能夠幫你解決所遇到的問題。

      如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。