资源预览内容
第1页 / 共6页
第2页 / 共6页
第3页 / 共6页
第4页 / 共6页
第5页 / 共6页
第6页 / 共6页
亲,该文档总共6页全部预览完了,如果喜欢就下载吧!
资源描述
本文档如对你有帮助,请帮忙下载支持!这个是根据上次框架版本进行的优化用 python 获取 excel 文件中测试用例数据通过 requets测试接口、并使用正则表达式验证响应信息内容生成 xml文件测试报告版本更新内容:1.整理了CreateTest.test_main()流程逻辑2. 优化了 testcase.xls文件格式3. 添加了生成 XML 文件测试报告代码如下:1 #!/usr/bin/env python2 # -*- coding: utf_8 -*-3 # 获取测试用例文件 excel45 import xlrd6 import json789 class CreateExcel:10def _init_(self):11pass1213 classmethod14 def open_excel(cls):15path =testcase.xls16 workbook = xlrd.open_workbook(path)17 table = workbook.sheets()018returntable1920 # 获取 sheet2122 classmethod23 def get_nrows(cls, table):24 nrows = table.nrows25 return nrows2627 # 获取行号2829 classmethod30 def get_id(cls, table, nrows):31 testid = 32for i in range(1, nrows):33testid.append(table.cell(i, 0).value)本文档如对你有帮助,请帮忙下载支持!34return testid3536 classmethod37 def get_name(cls, table, nrows):38 testname = 39for iin range(1, nrows):40testname.append(table.cell(i, 1).value)41returntestname4243 # 获取用例 name4445 classmethod46 def get_data(cls, table, nrows):47 testdata = 48for iin range(1, nrows):49try :50data = json.loads(table.cell(i, 2).value)51testdata.append(data)52except ValueError:53testdata.append(None)54returntestdata5556 # 获取 data 接口参数5758 classmethod59 def get_url(cls, table, nrows):60 testurl = 61for iin range(1, nrows):62testurl.append(table.cell(i, 3).value)63returntesturl6465 # 获取接口测试 url6667 classmethod68 def get_method(cls, table, nrows):69 testmethod = 70for iin range(1, nrows):71testmethod.append(table.cell(i, 4).value)72returntestmethod7374 # 获取接口测试 method7576 classmethod77 def get_pattern(cls, table, nrows):78 testpattern = 79for iin range(1, nrows):80testpattern.append(table.cell(i, 5).value)81returntestpattern本文档如对你有帮助,请帮忙下载支持!8283 # 获取接口期望响应结果841 #!/usr/bin/env python2 # -*- coding: utf_8 -*-3 # 测试核心组件45 import requests6 import re7 from datetime import datetime8from createexcelimport CreateExcel9 from xml.dom import minidom10 import sys111213 class CreateTest:14 reload(sys)15sys.setdefaultencoding(utf-8)1617 # 避免字符串写入文件出错1819def _init_(self):20pass2122 classmethod23 def test_api(cls, method, url, data):24 global results25try :26if method =post :27results = requests.post(url, data)28if method =get :29results = requests.get(url, data)30returnresults31except Exception._bases_:32print 服务器访问失败 3334 # 接口函数3536 classmethod37 def test_on(cls):38print 用例执行开始 3940 classmethod41 def test_close(cls):42print 用例执行结束 4344 classmethod45 def test_result(cls, pa):本文档如对你有帮助,请帮忙下载支持!46globalreport47try :48pattern = re.compile(pa)49match = pattern.search(testresults.text)50ifmatch.group() = pa:51report = 测试通过 52exceptAttributeError:53report = 测试失败 54returnreport5556 # 正则表达式检测5758 classmethod59 def test_http(cls, code):60print 请求返回状态码: , code6162 classmethod63 def test_time(cls):64 nowtime = datetime.today()65time = nowtime.strftime(%Y-%m-%d %H:%M:%S)66return time6768 # 获取当前时间转化字符串6970 classmethod71 def test_report(cls):72 nowtime = datetime.today()73reportime = nowtime.strftime(%Y%m%d%H%M%S)74reportname = reportime +.xml75return reportname7677 # 获取测试报告文件名称7879 classmethod80 def test_main(cls):81 global testresults82 table = CreateExcel.open_excel()83 nrows = CreateExcel.get_nrows(table)84xml = minidom.Document()85xml.appendChild(xml.createComment( 测试报告 )86caselist = xml.createElement(caselist )87xml.appendChild(caselist)88for i in range(0, nrows - 1):89testid = CreateExcel.get_id(table, nrows)i90testname = CreateExcel.get_name(table, nrows)i91testdata = CreateExcel.get_data(table, nrows)i92testurl = CreateExcel.get_url(table, nrows)i93testmethod = CreateExcel.get_method(table, nrows)i本文档如对你有帮助,请帮忙下载支持!94testpattern = CreateExcel.get_pattern(table, nrows)i9596# 执行测试97CreateTest.test_on()98testresults = CreateTest.test_api(testmethod, testurl, testdata)99testcode = str(testresults.status_code)100try :101CreateTest.test_http(testresults.status_code)102except AttributeError:103pass104CreateTest.test_close()105# 执行结束106
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号