當前位置:編程學習大全網 - 源碼下載 - 使用python做接口自動化測試容易嗎

使用python做接口自動化測試容易嗎

為?麽要做接口自動化測試?

在當前互聯網產品叠代頻繁的背景下,回歸測試的時間越來越少,很難在每個叠代都對所有功能做完整回歸。但接口自動化測試因其實現簡單、維護成本低,容?提高覆蓋率等特點,越來越受重視。

為?麽要自己寫框架呢?

使用Postman調試通過過直接可以獲取接口測試的基本代碼,結合使用requets + unittest很容?實現接口自動化測試的封裝,而且requests的api已經非常人性化,非常簡單,但通過封裝以後(特別是針對公司內特定接口),可以進壹步提高腳本編寫效率。

壹個現有的簡單接口?子

下面使用requests + unittest測試壹個查詢接口

接口信息如下

請求信息:

Method:POST

URL:api/match/image/getjson

Request:

{

"category": "image",

"offset": "0",

"limit": "30",

"sourceId": "0",

"metaTitle": "",

"metaId": "0",

"classify": "unclassify",

"startTime": "",

"endTime": "",

"createStart": "",

"createEnd": "",

"sourceType": "",

"isTracking": "true",

"metaGroup": "",

"companyId": "0",

"lastDays": "1",

"author": ""

}

Response示例:

{

"timestamp" : xxx,

"errorMsg" : "",

"data" : {

"config" : xxx

}

Postman測試方法見截圖:

測試思路

1.獲取Postman原始腳本

2.使用requests庫模擬發送HTTP請求**

3.對原始腳本進行基礎改造**

4.使用python標準庫?unittest寫測試case**

原始腳本實現

未優化

該代碼只是簡單的壹次調用,而且返回的結果太多,很多返回信息暫時沒用,示例代碼如下

import requests

url = "/api/match/image/getjson"

querystring = {"category":"image","offset":"0","limit":"30","sourceId":"0","metaTitle":"","metaId":"0","classify":"unclassify","startTime":"","endTime":"","createStart":"","createEnd":"","sourceType":"","isTracking":"true","metaGroup":"","companyId":"0","lastDays":"1","author":""}

headers = { 'cache-control': "no-cache", 'postman-token': "e97a99b0-424b-b2a5-7602-22cd50223c15"

}

response = requests.request("POST", url, headers=headers, params=querystring)

print(response.text)

優化 第壹版

調整代碼結構,輸出結果Json出來,獲取需要驗證的response.status_code,以及獲取結果校驗需要用到的results['total']

#!/usr/bin/env python#coding: utf-8'''

unittest merchant backgroud interface

@author: zhang_jin

@version: 1.0

@see:/api/match/image/getjson"

querystring = { "category": "image", "offset": "0", "limit": "30", "sourceId": "0", "metaTitle": "", "metaId": "0", "classify": "unclassify", "startTime": "", "endTime": "", "createStart": "", "createEnd": "", "sourceType": "", "isTracking": "true", "metaGroup": "", "companyId": "0", "lastDays": "1", "author": ""

}

headers = { 'cache-control': "no-cache", 'postman-token': "e97a99b0-424b-b2a5-7602-22cd50223c15"

}#Post接口調用

response = requests.request("POST", url, headers=headers, params=querystring)#對返回結果進行轉義成json串

results = json.loads(response.text)#獲取/api/match/image/getjson"

querystring = { "category": "image", "offset": "0", "limit": "30", "sourceId": "0", "metaTitle": "", "metaId": "0", "classify": "unclassify", "startTime": "", "endTime": "", "createStart": "", "createEnd": "", "sourceType": "", "isTracking": "true", "metaGroup": "", "companyId": "0", "lastDays": "1", "author": ""

}

headers = { 'cache-control': "no-cache", 'postman-token': "e97a99b0-424b-b2a5-7602-22cd50223c15"

}try: #Post接口調用

response = requests.request("POST", url, headers=headers, params=querystring) #對http返回值進行判斷,對於200做基本校驗 if response.status_code == 200:

results = json.loads(response.text) if results['total'] == 191: print "Success" else: print "Fail" print results['total'] else: #對於http返回非200的code,輸出相應的code raise Exception("http error info:%s" %response.status_code)except:

traceback.print_exc()

  • 上一篇:什麽是網絡信息化?
  • 下一篇:求好看電影
  • copyright 2024編程學習大全網