環境准備
python+requests
讀取企業微信api開發文檔,得知調用企業微信接口必須先獲取企業微信的accesstoken是通過 ("corpid","") //企業id 和 ("corpsecret","") //企業密鑰 調用獲取,python代碼實現如下
requests_url='https://qyapi.weixin.qq.com/cgi-bin/gettoken?' # 獲取token的接口地址
requests_data={"corpid":"ww2371688596076","corpsecret":"okaj3sQZSGneNs4IHkp5RD8j3v_7ZSa8IHF6Y"}
res=requests.get(requests_url,requests_data)
print("text解析的結果",res.text)
print("json解析的結果",res.json)

封裝請求方法
class httpRequest:
def http_request(self,url,data,http_method): #定義接口請求方法
#寫法1:
if http_method.upper()=='GET':
try:
res=requests.get(url,data)
except Exception as e:
print("get請求報錯:{0}".format(e))
raise # 拋出異常
elif http_method.upper()=='POST':
try:
res = requests.get(url, data)
except Exception as e:
print("post請求報錯:{0}".format(e))
raise # 拋出異常
else:
print("請求方法錯誤")
return res # 返回結果
調用封裝的方法請求獲取token
#主方法入口
if __name__ == '__main__':
res2=httpRequest().http_request(requests_url,requests_data,'post')
print("測試返回的token為:{}".format(res2))
整個方法如下:
#獲取企業微信的接口地址,獲取對應token
requests_url='https://qyapi.weixin.qq.com/cgi-bin/gettoken?'
requests_data={"corpid":"ww237168859620176","corpsecret":"okaj3sQJmGneNjh4s4IHkp5RD8j3v_7ZSa8IHF6Y"}
res=requests.get(requests_url,requests_data)
class httpRequest:
def http_request(self,url,data,http_method): #定義接口請求方法
#寫法1:
if http_method.upper()=='GET':
try:
res=requests.get(url,data)
except Exception as e:
print("get請求報錯:{0}".format(e))
raise # 拋出異常
elif http_method.upper()=='POST':
try:
res = requests.get(url, data)
except Exception as e:
print("post請求報錯:{0}".format(e))
raise # 拋出異常
else:
print("請求方法錯誤")
return res # 返回結果
#寫法2
# try:
# if http_method.upper() == 'GET':
# res=requests.get(url,data)
# elif http_method.upper() == 'POST':
# res = requests.get(url, data)
# except Exception as e:
# print("請求方法錯誤.{e}".format(e))
# raise e
# return res
#主方法入口
if __name__ == '__main__':
res2=httpRequest().http_request(requests_url1,requests_data,'post')
print("測試返回的結果:{}".format(res2.text))
print("測試返回的token為:{}".format(json.loads(res2.text)["access_token"])) # loads操作字符串獲取對應的key

代碼架構

執行類:
#代碼啟動類
from tools.http_request import httpRequest as testmethod # 導入http_request取別名
from tools.DoExcel import DoExcel
import json
def run(test_data): #列表嵌套的字典數據格式
for item in test_data: # for循環獲取 testdata 數據 ,並執行用來
print("正在測試用例為{0}".format(item['title']))
login_res=testmethod().http_request(item["url"],item["data"],item["http_method"])
print("請求的結果是:{0}".format(login_res.json()))
test_data = [{"url": "https://qyapi.weixin.qq.com/cgi-bin/gettoken?",
"data": {"corpid": "ww2371688596201076",
"corpsecret": "okaj3sQZSWJmGneNjh4s4IHkp5R8j3v_7ZSa8IHF6Y"},
"title":"正確獲取token","http_method":"get"},
{"url": "https://qyapi.weixin.qq.com/cgi-bin/gettoken?",
"data": {"corpid": "", "corpsecret": "okaj3sQZSWJmGneNjh4s4Hkp5RD8j3v_7ZSa8IHF6Y"},
"title":"參數錯誤","http_method":"get"}]
run(test_data)
執行結果:

數據驅動
將接口測試用例存放在excel中

獲取excel數據的方法
#從xls 讀取信息
from openpyxl import load_workbook
class DoExcel:
def get_data(self,file_name,sheet_name):
wb=load_workbook(file_name)
sheet=wb[sheet_name]
# 遍歷獲取數據
test_data=[] #初始化
for i in range(2, sheet.max_row+1):
row_data={}
row_data['case_id']=sheet.cell(i,1).value #獲取1列的值
row_data['url']=sheet.cell(i,2).value #獲取2列的值
row_data['data']=sheet.cell(i,3).value #獲取3列的值
row_data['title']=sheet.cell(i,4).value #獲取4列的值
row_data['http_method'] = sheet.cell(i, 5).value # 獲取5列的值
test_data.append(row_data)
return test_data
#回寫返回的結果值到excle
def write_back(self,file_name,sheet_name,i,value):
wb=load_workbook(file_name)
sheet=wb[sheet_name]
sheet.cell(i,6).value=value # 寫死第6列 返回的結果
wb.save(file_name) #保存結果
if __name__ == '__main__':
data=DoExcel().get_data("../test_data/data1.xlsx","login") # 傳入filename 與 sheet名
print(data)
