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)

 正则表达式匹配

1 用import.re导入正则表达式模块
2 用re.complie()函数创建一个Regex对象(记得使用原始字符串)
3 向Regex对象的search()方法传入想查找的字符串。它返回一个Match对象
4 调用Macth对象的group()方法,返回实际匹配文本的字符串

 

posted @ 2019-01-14 15:02  wutantan  阅读(520)  评论(0)    收藏  举报