本節介紹,使用python實現接口自動化實現。
思路:講接口數據存放在excel文檔中,讀取excel數據,將每一行數據存放在一個個列表當中。然后獲取URL,header,請求體等數據,進行請求發送。
結構如下

excel文檔內容如下:

一、Common與Config包
Config里面的config.ini主要存放的默認的路徑內容等,如excel文件的地址,工作簿名稱

Common里面主要是一些通用的方法,目前只需要讀取config里面的數據
import configparser
import os.path
#讀取config里面的數據
class ReadConfig:
def __init__(self):
self.filePath = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))+"\\config\\config.ini"
print(self.filePath)
self.config = configparser.ConfigParser()
self.config.read(self.filePath,encoding='utf-8')
def get_data(self,section,key):
return self.config.get(section,key)
def get_list(self,section):
return self.config.items(section)
if __name__ == '__main__':
conf = ReadConfig()
print(conf.get_data("excel", "file_path"))
二、excel包
主要是實現讀取excel表格數據,此處用的是openpyxl進行實現
import openpyxl
class ReadExcel:
def __init__(self,excel_path,sheet_name):
self.excel_path = excel_path
self.sheet_name = sheet_name
def get_data(self,row):
workbook = openpyxl.load_workbook(self.excel_path)
sh = workbook[self.sheet_name]
data = []
for c in list(sh.rows)[row]:
data.append(c.value)
return data
if __name__ == '__main__':
ex = ReadExcel('request.xlsx','Sheet1')
print(ex.get_data(0))
print(ex.get_data(1))
print(ex.get_data(2))
print(ex.get_data(3))
print(ex.get_data(4))
三、requests包
主要用於發送請求,這里只寫了常用的get post請求,需要可以加其他的。
由於post請求體有多種,此處只區分了兩種,其他的可以加上。
import json
import requests
class Request:
def get_url(self,url,headers=None,paras=None):
if url==None:
print("URL地址為空")
elif paras == None:
r = requests.get(url,headers=headers,params=paras)
else:
r = requests.get(url, headers=headers, params=json.loads(paras))
return r
def post_url(self,url,content_type,headers=None,payload=None):
if url==None:
print("URL地址為空")
else:
if content_type == "application/json":
payload_json = json.dumps(payload)
r = requests.post(url,headers=headers,data=payload_json)
elif content_type =="application/x-www-form-urlencoded":
r = requests.post(url,headers=headers,data=payload)
else:
print("no this content-type")
return r
def choose_method(self,method,url,content_type,headers=None,payload=None):
if method == "get":
return self.get_url(url,headers,payload)
elif method == "post":
return self.post_url(url,content_type,headers,payload)
else:
print("no this method request")
四、testcases包
使用pytest框架進行自動化測試
import json
import pytest
from autoStruct.apiAuto.common.requests import Request
from autoStruct.apiAutoTesting.common.readConfig import ReadConfig
from autoStruct.apiAutoTesting.excel.readExcel import ReadExcel
class TestApi:
def setup_class(self):
file_path = ReadConfig().get_data('excel', 'file_path')
sheet_name = ReadConfig().get_data('excel', 'sheet_name')
self.ex = ReadExcel(file_path, sheet_name)
@pytest.mark.parametrize('num',[1,2,3,4,5])
def testcase(self,num):
data = self.ex.get_data(num)
print(data)
if data[3]==None:
r = Request().choose_method(data[1],data[0],data[4],json.loads(data[2]),data[3])
else:
r = Request().choose_method(data[1], data[0], data[4], json.loads(data[2]), json.loads(data[3]))
print(r.text)
if __name__ == '__main__':
pytest.main(['-vs','testapi.py'])
