背景:
隨着測試逐漸優化升級,單個每回調用一次接口,有時候會滿足不了要求,打個比方:像測試人臉識別算法的識別率(讓開發暴露出了一個rest接口給了我),就需要調用大量的照片訪問這個接口,已達到目的,那現在這種情況下就不能一張一張的來調用接口,之前的postman就不能滿足這個需求,所以就用到了下面的方法,接下來就簡單給大家說一下。
步驟:
首先,先收集照片,case裏的滿足各種條件的照片,放置於不同的文件目錄下;
然後,與開發對過或者查看接口文檔中的傳參,弄清傳參格式。這裏講述的是一個簡單的只傳了base64格式的照片形式,遍歷文件夾下的圖片,然後將圖片形式寫成base64格式,代碼如下:
# coding=utf-8
import requests
import os
import cv2
import base64
def get_facename(self, file_path):
list_name = []
file_names = os.listdir(file_path)
for file_name in file_names:
if os.path.isfile(os.path.join(file_path, file_name)):
list_name.append(file_name)
return list_name
def get_img_base64(self, file_path):
list_base64 = []
for file_name in Aaf().get_facename(file_path):
img = cv2.imread(file_path + file_name)
img_str = cv2.imencode('.jpg', img)[1].tostring()
img_base64 = base64.b64encode(img_str)
# print img_base64
list_base64.append(img_base64)
return list_base64
然後,將圖片編碼之後的base64格式導入到接口中。
def face_att(self, file_path, post_url):
for img in Aaf().get_img_base64(file_path):
payload = "{\n\t\"image\":\"" + str(img) + "\"\r\n}"
headers = {
#響應頭,在postman中可以得到,可寫可不寫
}
try:
response = requests.request("POST", url, data=payload, headers=headers)
print response.text
except Exception as error:
print error
最後,輸出的信息,可以按照自己的需求來進行分析操作,比如寫到文件中,再分析文件裏的對應需求,再比如,將不同結果寫到不同文件裏....。
綜合來講:
一共就分爲,遍歷各個文件下的照片,將照片轉成base64形式,再把這些參數傳進入接口(這裏的參數我只傳了一個base64格式的照片,可根據自己的需要來傳參數)。
以上就是這些,大家有什麼好的意見和建議可以跟我聊哦。