python 介面自動化 excel引數化封裝
阿新 • • 發佈:2018-11-21
1、定義config 檔案
裡面定義全域性用的引數, 比如下面用到的 excel 的地址資訊。
config定義如下
# !/usr/bin/python3 # -*- coding: utf-8 -*- import os # 路徑配置;專案的路徑 BASE_PATH = os.path.dirname( os.path.dirname(os.path.abspath(__file__))) # 專案對應的資料夾路徑,拼接存放測試用例的路徑 CASE_PATH = os.path.join(BASE_PATH, 'Testcase')#定義資料夾路徑,遍歷資料夾下的用例資訊。 Log_PATH= os.path.join(BASE_PATH, 'log')#定義log 的生成路徑 #定義EXCEl檔案路徑 path = r'D:\Workspace\BItestData\Testcase\api.xls'
2、定義excel 的解析方式,註釋部分的程式碼是單獨獲取某行某列的引數。
匯入config 配置的path 路徑,獲取到excel路徑及檔名
根據自己的excel 各列的要求定義相關列的獲取方法。
# !/usr/bin/python3 # -*- coding: utf-8 -*- import os import xlrd import Util.config as cf class OpeExcel(object): def __init__(self, path): self.path = path # def __init__(self,file_name=None,sheet_id=None): # if file_name: # self.file_name = file_name # self.sheet_id = sheet_id # else: # self.file_name = r'D:\Workspace\BItestData\Testcase\api.xls' # self.sheet_id = 0 # self.data = self.get_data() # #獲取sheets的內容 # def get_data(self): # data = xlrd.open_workbook(self.file_name) # tables = data.sheets()[self.sheet_id] # return tables # #獲取單元格行數 # def get_lines(self): # tables = self.data # return tables.nrows # #獲取單元格資料 獲取 # def get_value(self,row,col): # return self.data.cell_value(row,col) @property def getSheet(self): # 獲取索引 獲取excel 當前sheet xl = xlrd.open_workbook(self.path) sheet = xl.sheet_by_index(0) return sheet @property def getRows(self): # 獲取行數 row = self.getSheet.nrows return row @property def getCol(self): # 獲取列數 col = self.getSheet.ncols return col # 以下是分別獲取每一列的數值 #獲取用例ID資訊 @property def getId(self): TestId = [] for i in range(1, self.getRows): TestId.append(self.getSheet.cell_value(i, 0)) return TestId # 獲取用例描述資訊 @property def getDes(self): TestDes = [] for i in range(1, self.getRows): TestDes.append(self.getSheet.cell_value(i, 1)) return TestDes #獲取URL地址資訊 @property def getUrl(self): TestUrl = [] for i in range(1, self.getRows): TestUrl.append(self.getSheet.cell_value(i, 2)) return TestUrl # 獲取用例請求引數資訊 @property def getData(self): TestData = [] for i in range(1, self.getRows): TestData.append(self.getSheet.cell_value(i, 4)) return TestData # 獲取請求方式資訊 @property def getMethod(self): TestMethod = [] for i in range(1, self.getRows): TestMethod.append(self.getSheet.cell_value(i, 3)) return TestMethod @property def getCode(self): TestCode = [] for i in range(1, self.getRows): TestCode.append(self.getSheet.cell_value(i, 5)) return TestCode if __name__ == '__main__': opers = OpeExcel(cf.path) print(opers.getId) //用例ID print(opers.getDes)//用例的描述 print(opers.getUrl)//請求地址 print(opers.getMethod)//請求方式 print(opers.getData)//請求的data資料 3、輸出資訊
4、 列印部分的資訊封裝成各請求list 資訊 帶入request 請求方式。