python——一些常用的方法类

测试的时候经常需要使用一些方法都整理放在一起,方便调用

首先一些基本的配置引入

 localReadConfig = readConfig.ReadConfig()
proDir = readConfig.proDir
log = Log.get_log() caseNo = 0

根据xls_name和sheet_name来读取数据

 def get_xls(xls_name, sheet_name):
"""
get interface data from xls file
:param xls_name:
:param sheet_name:
:return:
"""
cls = []
# get xls file's path
xls_path = os.path.join(proDir, 'testFile', 'case', xls_name)
# open xls file
file = xlrd.open_workbook(xls_path)
# get sheet by name
sheet = file.sheet_by_name(sheet_name)
# get one sheet's rows
nrows = sheet.nrows
for i in range(nrows):
if sheet.row_values(i)[0] != u'case_name':
cls.append(sheet.row_values(i))
return cls

将结果写入到excel中

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

正则表达式匹配

 用import.re导入正则表达式模块
用re.complie()函数创建一个Regex对象(记得使用原始字符串)
向Regex对象的search()方法传入想查找的字符串。它返回一个Match对象
调用Macth对象的group()方法,返回实际匹配文本的字符串
上一篇:Android API在不同版本系统上的兼容性


下一篇:Python中使用%还是format来格式化字符串?