您好,欢迎访问三七文档
当前位置:首页 > 财经/贸易 > 资产评估/会计 > python学习笔记-excel用例输入
python学习笔记(接口自动化框架V2.0)这个是根据上次框架版本进行的优化用python获取excel文件中测试用例数据通过requets测试接口、并使用正则表达式验证响应信息内容生成xml文件测试报告版本更新内容:1.整理了CreateTest.test_main()流程逻辑2.优化了testcase.xls文件格式3.添加了生成XML文件测试报告代码如下:1#!/usr/bin/envpython2#-*-coding:utf_8-*-3#获取测试用例文件excel45importxlrd6importjson789classCreateExcel:10def__init__(self):11pass1213@classmethod14defopen_excel(cls):15path=testcase.xls16workbook=xlrd.open_workbook(path)17table=workbook.sheets()[0]18returntable1920#获取sheet2122@classmethod23defget_nrows(cls,table):24nrows=table.nrows25returnnrows2627#获取行号2829@classmethod30defget_id(cls,table,nrows):31testid=[]32foriinrange(1,nrows):33testid.append(table.cell(i,0).value)34returntestid3536@classmethod37defget_name(cls,table,nrows):38testname=[]39foriinrange(1,nrows):40testname.append(table.cell(i,1).value)41returntestname4243#获取用例name4445@classmethod46defget_data(cls,table,nrows):47testdata=[]48foriinrange(1,nrows):49try:50data=json.loads(table.cell(i,2).value)51testdata.append(data)52exceptValueError:53testdata.append(None)54returntestdata5556#获取data接口参数5758@classmethod59defget_url(cls,table,nrows):60testurl=[]61foriinrange(1,nrows):62testurl.append(table.cell(i,3).value)63returntesturl6465#获取接口测试url6667@classmethod68defget_method(cls,table,nrows):69testmethod=[]70foriinrange(1,nrows):71testmethod.append(table.cell(i,4).value)72returntestmethod7374#获取接口测试method7576@classmethod77defget_pattern(cls,table,nrows):78testpattern=[]79foriinrange(1,nrows):80testpattern.append(table.cell(i,5).value)81returntestpattern8283#获取接口期望响应结果841#!/usr/bin/envpython2#-*-coding:utf_8-*-3#测试核心组件45importrequests6importre7fromdatetimeimportdatetime8fromcreateexcelimportCreateExcel9fromxml.domimportminidom10importsys111213classCreateTest:14reload(sys)15sys.setdefaultencoding(utf-8)1617#避免字符串写入文件出错1819def__init__(self):20pass2122@classmethod23deftest_api(cls,method,url,data):24globalresults25try:26ifmethod==post:27results=requests.post(url,data)28ifmethod==get:29results=requests.get(url,data)30returnresults31exceptException.__bases__:32print服务器访问失败3334#接口函数3536@classmethod37deftest_on(cls):38print用例执行开始3940@classmethod41deftest_close(cls):42print用例执行结束4344@classmethod45deftest_result(cls,pa):46globalreport47try:48pattern=re.compile(pa)49match=pattern.search(testresults.text)50ifmatch.group()==pa:51report=测试通过52exceptAttributeError:53report=测试失败54returnreport5556#正则表达式检测5758@classmethod59deftest_http(cls,code):60print请求返回状态码:,code6162@classmethod63deftest_time(cls):64nowtime=datetime.today()65time=nowtime.strftime(%Y-%m-%d%H:%M:%S)66returntime6768#获取当前时间转化字符串6970@classmethod71deftest_report(cls):72nowtime=datetime.today()73reportime=nowtime.strftime(%Y%m%d%H%M%S)74reportname=reportime+.xml75returnreportname7677#获取测试报告文件名称7879@classmethod80deftest_main(cls):81globaltestresults82table=CreateExcel.open_excel()83nrows=CreateExcel.get_nrows(table)84xml=minidom.Document()85xml.appendChild(xml.createComment(测试报告))86caselist=xml.createElement(caselist)87xml.appendChild(caselist)88foriinrange(0,nrows-1):89testid=CreateExcel.get_id(table,nrows)[i]90testname=CreateExcel.get_name(table,nrows)[i]91testdata=CreateExcel.get_data(table,nrows)[i]92testurl=CreateExcel.get_url(table,nrows)[i]93testmethod=CreateExcel.get_method(table,nrows)[i]94testpattern=CreateExcel.get_pattern(table,nrows)[i]9596#执行测试97CreateTest.test_on()98testresults=CreateTest.test_api(testmethod,testurl,testdata)99testcode=str(testresults.status_code)100try:101CreateTest.test_http(testresults.status_code)102exceptAttributeError:103pass104CreateTest.test_close()105#执行结束106#生成xml文件107case=xml.createElement(case)108case.setAttribute(id,testid)109#输入用例ID110111name=xml.createElement(name)112name.appendChild(xml.createTextNode(testname))113#输入用例名称114method=xml.createElement(method)115method.appendChild(xml.createTextNode(testmethod))116#输入接口类型117code=xml.createElement(code)118code.appendChild((xml.createTextNode(testcode)))119#输入用例返回状态码120result=xml.createElement(result)121result.appendChild(xml.createTextNode(CreateTest.test_result(testpattern)))122#输入用例测试结果123time=xml.createElement(time)124time.appendChild(xml.createTextNode(CreateTest.test_time()))125#输入用例执行时间126127case.appendChild(name)128case.appendChild(method)129case.appendChild(code)130case.appendChild(result)131case.appendChild(time)132133caselist.appendChild(case)134#xml文件生成结束135filename=file(CreateTest.test_report(),w+)136#生成以当前时间命名的测试报告文件137xml.writexml(filename)138filename.close()139#关闭文件140141142if__name__=='__main__':143CreateTest.test_main()下面是测试入口:1#!/usr/bin/envpython2#-*-coding:utf_8-*-3#****************************************************************4#interface.py5#Author:ChenLei6#Version:2.07#Date:2016-4-158#****************************************************************910importtime11fromcreatetestimportCreateTest1213start=time.clock()14CreateTest.test_main()15end=time.clock()1617print接口自动化脚本运行时间:%.03fseconds%(end-start)运行后自动生成当前时间的xml文件如下:分类:python
本文标题:python学习笔记-excel用例输入
链接地址:https://www.777doc.com/doc-4355057 .html