1. 程式人生 > >python——一些常用的方法類

python——一些常用的方法類

測試的時候經常需要使用一些方法都整理放在一起,方便呼叫

首先一些基本的配置引入

1 localReadConfig = readConfig.ReadConfig()
2 proDir = readConfig.proDir
3 log = Log.get_log()
4 
5 caseNo = 0

根據xls_name和sheet_name來讀取資料

 1 def get_xls(xls_name, sheet_name):
 2     """
 3     get interface data from xls file
 4     :param xls_name:
5 :param sheet_name: 6 :return: 7 """ 8 cls = [] 9 # get xls file's path 10 xls_path = os.path.join(proDir, 'testFile', 'case', xls_name) 11 # open xls file 12 file = xlrd.open_workbook(xls_path) 13 # get sheet by name 14 sheet = file.sheet_by_name(sheet_name)
15 # get one sheet's rows 16 nrows = sheet.nrows 17 for i in range(nrows): 18 if sheet.row_values(i)[0] != u'case_name': 19 cls.append(sheet.row_values(i)) 20 return cls

將結果寫入到excel中

1 def write_result_into_xls(xls_path, sheet, wb, result, row, col):
2     sheet.write(row, col, result)
3    wb.save(xls_path)
1 def write_results_into_xls(xls_name, sheet_name, results, col):
2     xls_path = os.path.join(proDir, 'testFile', 'case', xls_name)
3     rb = xlrd.open_workbook(xls_path)
4     wb = copy.copy(rb)
5     sheet = wb.get_sheet(sheet_name)
6     for i in range(len(results)):
7         write_result_into_xls(xls_path, sheet, wb, results[i], i+1, col)