按关键词阅读: 学习 Python 输入 Excel 笔记
1、本文档如对你有帮助 , 请帮忙下载支持!81这个是根据上次框架版本进行的优化 用python 获取excel文件中测试用例数据 通过requets 测试接口、并使用正则表达式验证响应信息内容 生成xml文件测试报告 版本更新内容:1.整理了 CreateTest.test_mainO 流程逻辑2.优化了 testcase.xls文件格式3.添加了生成XML文件测试报告代码如下:123456789101112131415161718192021222324252627282930313233#!/usr/b in/env py tho n# -*- cod ing: utf_8 -*-#获取测试用例 。
2、文件excelimport xlrd import jsonclass CreateExcel: def __init_ (self): p assclassmethoddef open _excel(cls):p ath = testcase.xlsworkbook = xlrd. open _workbook( path) table = workbook.sheets()0return table#获取sheetclassmethoddef get_ nrows(cls, table): n rows = table .n rows return nrows#获取行号classmetho 。
【python|python学习笔记excel用例输入】3、ddef get_id(cls, table, n rows): testid =for i in range(1, nrows):testid.a ppen d(table.cell(i, 0).value)3435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980return testidclassmethoddef get_ name(cls, table, n rows):test name =for i in range(1, nrows):test na 。
4、me.a ppen d(table.cell(i, 1).value) return test name#获取用例nameclassmethoddef get_data(cls, table, n rows): testdata =https://www.renrendoc.com/paper/for i in range(1, nrows):try :data = json.l oads(table.cell(i, 2).value) testdata.a ppen d(data)exce pt ValueError: testdata.a ppen d(N one) return testdata#获取data接口参数classmethoddef ge 。
5、t_url(cls, table, n rows): testurl =for i in range(1, nrows): testurl.a ppen d(table.cell(i, 3).value) return testurl#获取接口测试urlclassmethoddef get_method(cls, table, n rows): testmethod =for i in range(1, nrows): testmethod.a ppen d(table.cell(i, 4).value) return testmethod#获取接口测试 methodclassmethodde 。
6、f get_ patter n( cls, table, n rows):test pattern =for i in range(1, nrows):test pattern.a ppen d(table.cell(i, 5).value) return test patter n本文档如对你有帮助 , 请帮忙下载支持!828384123456789101112131415161718192021222324252627282930313233343536373839404142434445#获取接口期望响应结果#!/usr/b in/env py tho n# -*- coding: utf_ 。
7、8 -*-#测试核心组件import requestsimport refrom datetime import datetimefrom createexcel import CreateExcel from xml.dom import minidom import sysclass CreateTest: reload(sys) sys.setdefaulte ncod ing(#避免字符串写入文件出错def __init_ (self): p assutf-8)classmethoddef test_a pi (cls, method, url, data): global resul 。
8、ts try :if method = post: results = requests .p ost(url, data)if method = get: results = requests.get(url, data) return resultsexcept Exception. __bases__: print服务器访问失败卞#接口函数classmethoddef test_o n( cls):print用例执行开始classmethoddef test_close(cls):print 用例执行结束classmethoddef test_result(cls, p a):46474 。
9、8495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293global reporttry :p atter n = re.co mp ile( pa)match = p attern.search(testresults.text)if match.group() = pa:report =测试通过exce pt AttributeError:report =测试失败return report#正则表达式检测classmethoddef test_htt p( cls, 。
10、 code): print 请求返回状态码:classmethoddef test_time(cls):no wtime = datetime.today() time = no wtime.strftime( return time#获取当前时间转化字符串classmethoddef test_re port(cls):no wtime = datetime.today() rep ortime = no wtime.strftime( reportn ame = rep ortime + return reportname#获取测试报告文件名称,code%Y-%m-%d %H:%M:%S% 。
11、Y%m%d%H%M%S.xmlclassmethoddef test_mai n(cls):global testresultstable = CreateExcel.o pen _excel()n rows = CreateExcel.get_ nrows(table)xml = mi nidom.Docume nt()xml.appendChild(xml.createComment(测试报告)caselist = xml.createEleme nt(caselist )xml.a ppen dChild(caselist)for i in range(0, nrows - 1):tes 。
12、tid = CreateExcel.get_id(table, n rows)itest name = CreateExcel.get_ name(table, n rows)i testdata = https://www.renrendoc.com/paper/CreateExcel.get_data(table, n rows)i testurl = CreateExcel.get_url(table, n rows)i testmethod = CreateExcel.get_method(table, n rows)itest patter n = CreateExcel.get_ patter n(table, n rows)i94#执行测试C 。
13、reateTest.test_ on()testresults = CreateTest.test_a pi(testmethod, testurl, testdata) testcode = str(testresults.status_code)try :CreateTest.test_htt p(testresults.status_code)except AttributeError:p assCreateTest.test_close()#执行结束#生成xml文件case = xml.createEleme nt( case.setAttribute(id#输入用例ID9596979 。
14、899100101102103104105case) ,testid)106107108109110n ame)n ame = xml.createEleme nt(n ame.a ppen dChild(xml.createTextNode(test name)#输入用例名称method = xml.createEleme nt(method)method.a ppen dChild(xml.createTextNode(testmethod)#输入接口类型code = xml.createElement(code)code.a ppen dChild(xml.createTextNode( 。
来源:(未知)
【学习资料】网址:/a/2021/0330/0021814902.html
标题:python|python学习笔记excel用例输入