轉自:https://www.freesion.com/article/3046426995/
1.為了更好的實現讀取excel文件進行接口自動化測試,將操作excel文件方法封裝:
-
# coding:utf-8
-
import xlrd
-
from xlutils.copy
import copy
# 導入xlutils的copy方法
-
-
class HandleExcel:
-
"""封裝操作excel的方法"""
-
def __init__(self, file='E:/PyChram項目集合/interfacetest/excel/30.xls', sheet_id=0):
-
self.file = file
-
self.sheet_id = sheet_id
-
self.data = self.get_data()
-
# 為了在創建一個實例時就獲得excel的sheet對象,可以在構造器中調用get_data()
-
# 因為類在實例化時就會自動調用構造器,這樣在創建一個實例時就會自動獲得sheet對象了
-
-
# 獲取某一頁sheet對象
-
def get_data(self):
-
data = xlrd.open_workbook(self.file)
-
sheet = data.sheet_by_index(self.sheet_id)
-
return sheet
-
-
# 獲取excel數據行數
-
def get_rows(self):
-
rows = self.data.nrows
-
# t = self.get_data() # 調用get_data()取得sheet對象(如果不在構造器獲取sheet對象,就需要在方法內先獲取sheet對象,再進行下一步操作,每個方法都要這樣,所以還是寫在構造器中方便)
-
# rows = t.nrows
-
return rows
-
-
# 獲取某個單元格數據
-
def get_value(self, row, col):
-
value = self.data.cell_value(row, col)
-
return value
-
-
# 向某個單元格寫入數據
-
def write_value(self, row, col, value):
-
data = xlrd.open_workbook(self.file)
# 打開文件
-
data_copy = copy(data)
# 復制原文件
-
sheet = data_copy.get_sheet(
0)
# 取得復制文件的sheet對象
-
sheet.write(row, col, value)
# 在某一單元格寫入value
-
data_copy.save(self.file)
# 保存文件
-
-
# 封裝excel的列名常量
-
def get_caseseq():
-
"""獲取caseSeq"""
-
caseSeq =
0
-
return caseSeq
-
-
-
def get_apitype():
-
"""獲取apiType"""
-
apiType =
1
-
return apiType
-
-
-
def get_apiseq():
-
"""獲取apiSeq"""
-
apiSeq =
2
-
return apiSeq
-
-
-
def get_apiName():
-
"""獲取apiName"""
-
apiName =
3
-
return apiName
-
-
-
def get_priority():
-
"""獲取priority"""
-
priority =
4
-
return priority
-
-
-
def get_url():
-
"""獲取url"""
-
url =
5
-
return url
-
-
-
def get_method():
-
"""獲取method"""
-
method =
6
-
return method
-
-
-
def get_header():
-
"""獲取header"""
-
header =
7
-
return header
-
-
-
def get_purpose():
-
purpose =
8
-
return purpose
-
-
-
def get_params():
-
"""獲取params"""
-
params =
9
-
return params
-
-
-
def get_expectvalue():
-
"""獲取expectValue"""
-
expect =
10
-
return expect
-
-
def get_resultvalue():
-
result =
11
-
return result
-
-
if __name__ ==
'__main__':
-
test = HandleExcel()
-
print(test.get_data())
-
print(test.get_rows())
-
print(test.get_value(
0,
0))
2.將操作excel的方法封裝好后,准備接口用例來遍歷循環
其中:
caseSeq:用例編號
apiType:接口類型
apiSeq:接口編號
apiName:接口名稱
priority:優先級
url:接口調用地址
method:協議方法
header:請求頭
purpose:用例描述
params:接口參數
expectValue:期望結果
resultValue:測試結果
3.編寫主函數代碼,實現接口用例遍歷並將測試結果寫入excel表中
-
# coding:utf-8
-
-
from mylib.run_method
import RunMain
-
from mylib.handle_excel
import *
-
import json
-
-
class RunTestCase:
-
def __init__(self):
-
self.Runmain = RunMain()
# 實例化調用get/post請求基類
-
self.data = HandleExcel()
# 實例化操作excel文件類
-
-
def go_run(self):
-
rows_count = self.data.get_rows()
# 獲取excel行數
-
for i
in range(
1,rows_count):
# 利用行數進行迭代處理每個接口
-
url = self.data.get_value(i, get_url())
# 循環獲取url的值
-
print(url)
-
method = self.data.get_value(i, get_method())
# 循環獲取method的值
-
print(method)
-
if self.data.get_value(i, get_params()) ==
'':
-
data =
None
-
else:
-
data = json.loads(self.data.get_value(i, get_params()))
# 循環獲取請求參數,並將得到的數據反序列
-
expect = self.data.get_value(i, get_expectvalue())
# 循環獲取期望輸出
-
is_run = self.data.get_value(i, get_priority())
# 獲取是否運行,即判斷excel中priority是不是"H"
-
if is_run ==
'H':
-
res = self.Runmain.run_main(url, method, data)
# 調用get/post主函數
-
print(res)
-
if expect
in res:
-
print(
'測試成功')
-
self.data.write_value(i, get_resultvalue(),
'pass')
-
else:
-
print(
'測試失敗')
-
self.data.write_value(i, get_resultvalue(),
'fail')
-
-
if __name__ ==
'__main__':
-
run = RunTestCase()
-
run.go_run()
運行結果如下:
excel表格中填入如下:
本篇簡單的使用excel進行接口測試用例的執行,在測試過程中最重要的是測試用例的編寫,在遇到不同的測試用例執行時會出現不同的問題,還需要多使用多練習,按照實際進行Python接口自動化測試用例編寫。