封装操作Excel方法,后续功能进行直接调用
#!/usr/bin/env python3 # -*-coding:utf-8-*- # __author__: hunter import xlrd class HandleExcel: """封装操作Excel的方法""" def __init__(self, file=‘D:/hunter_/interfaceTest/interface/tool/demo2.xlsx‘, 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 return rows # 获取某个单元格写入数据 def get_value(self, row, col): value = self.data.cell_value(row, col) return value # 向某个单元格写入数据 def write_value(self): pass # 封装Excel的列名常量 def get_caseseq(): ‘‘‘获取caseSeq‘‘‘ caseSeq = 0 return caseSeq def get_apitype(): apiType = 1 return apiType def get_apiseq(): apiSeq = 2 return apiSeq def get_apiName(): apiName = 3 return apiName def get_priority(): priority = 4 return priority def get_url(): url = 5 return url def get_mothod(): mothod = 6 return mothod def get_header(): header = 7 return header def get_purpose(): purpose = 8 return purpose def get_params(): params = 9 return params def get_expectvalue(): expectvalue = 10 return expectvalue if __name__ == ‘__main__‘: test = HandleExcel() print(test.get_data()) print(test.get_rows()) print(test.get_value(0, 0))
Excel表格数据:
调用封装Excel的封装方法,进行循环遍历
#!/usr/bin/env python3 # -*-coding:utf-8-*- # __author__: hunter from conn.run_demo import RunMain from interface.tool.handle_excel import * 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) print(‘=====‘) method = self.data.get_value(i, get_mothod()) # 循环获取method的值 data = self.data.get_value(i, get_params()) # 循环获取请求参数 print(data) print(‘=====‘) is_run = self.data.get_value(i, get_priority()) # 获取是否运行,即判断Excel中priority是不是为“high" if is_run == ‘high‘: res = self.Runmain.run_main(url, method, data) # 调用主函数 print(res) print(‘=====‘) if __name__ == ‘__main__‘: run = RunTestCase() run.go_run()
运行:
接下来就是将测试结果写入Excel中,是否与预期结果一直
原文地址:https://www.cnblogs.com/hemingwei/p/11578496.html
时间: 2024-11-13 08:01:32