1、python学习笔记(接口自动化框架 V2.0)这个是依照上次框架版本进行优化用python获取excel文献中测试用例数据通过requets测试接口、并使用正则表达式验证响应信息内容生成xml文献测试报告版本更新内容:1. 整顿了CreateTest.test_main()流程逻辑2. 优化了testcase.xls文献格式3. 添加了生成XML文献测试报告代码如下: 1 #!/usr/bin/env python 2 # -*- coding:utf_8 -*- 3 # 获取测试用例文献excel 4 5 import xlrd 6 import json 7 8 9 class Crea
2、teExcel: 10 def _init_(self): 11 pass 12 13 classmethod 14 def open_excel(cls): 15 path = testcase.xls 16 workbook = xlrd.open_workbook(path) 17 table = workbook.sheets()0 18 return table 19 20 # 获取sheet 21 22 classmethod 23 def get_nrows(cls,table): 24 nrows = table.nrows 25 return nrows 26 27 # 获取
3、行号 28 29 classmethod 30 def get_id(cls,table,nrows): 31 testid = 32 for i in range(1,nrows): 33 testid.append(table.cell(i,0).value) 34 return testid 35 36 classmethod 37 def get_name(cls,table,nrows): 38 testname = 39 for i in range(1,nrows): 40 testname.append(table.cell(i,1).value) 41 return test
4、name 42 43 # 获取用例name 44 45 classmethod 46 def get_data(cls,table,nrows): 47 testdata = 48 for i in range(1,nrows): 49 try: 50 data = json.loads(table.cell(i,2).value) 51 testdata.append(data) 52 except ValueError: 53 testdata.append(None) 54 return testdata 55 56 # 获取data接口参数 57 58 classmethod 59 d
5、ef get_url(cls,table,nrows): 60 testurl = 61 for i in range(1,nrows): 62 testurl.append(table.cell(i,3).value) 63 return testurl 64 65 # 获取接口测试url 66 67 classmethod 68 def get_method(cls,table,nrows): 69 testmethod = 70 for i in range(1,nrows): 71 testmethod.append(table.cell(i,4).value) 72 return t
6、estmethod 73 74 # 获取接口测试method 75 76 classmethod 77 def get_pattern(cls,table,nrows): 78 testpattern = 79 for i in range(1,nrows): 80 testpattern.append(table.cell(i,5).value) 81 return testpattern 82 83 # 获取接口盼望响应成果 84 1 #!/usr/bin/env python 2 # -*- coding:utf_8 -*- 3 # 测试核心组件 4 5 import requests
7、6 import re 7 from datetime import datetime 8 from createexcel import CreateExcel 9 from xml.dom import minidom 10 import sys 11 12 13 class CreateTest: 14 reload(sys) 15 sys.setdefaultencoding(utf-8) 16 17 # 避免字符串写入文献出错 18 19 def _init_(self): 20 pass 21 22 classmethod 23 def test_api(cls,method,ur
8、l,data): 24 global results 25 try: 26 if method = post: 27 results = requests.post(url,data) 28 if method = get: 29 results = requests.get(url,data) 30 return results 31 except Exception._bases_: 32 print 服务器访问失败 33 34 # 接口函数 35 36 classmethod 37 def test_on(cls): 38 print 用例执行开始 39 40 classmethod 4
9、1 def test_close(cls): 42 print 用例执行结束 43 44 classmethod 45 def test_result(cls,pa): 46 global report 47 try: 48 pattern = pile(pa) 49 match = pattern.search(testresults.text) 50 if match.group() = pa: 51 report = 测试通过 52 except AttributeError: 53 report = 测试失败 54 return report 55 56 # 正则表达式检测 57 58
10、 classmethod 59 def test_http(cls,code): 60 print 祈求返回状态码: ,code 61 62 classmethod 63 def test_time(cls): 64 nowtime = datetime.today() 65 time = nowtime.strftime(%Y-%m-%d %H:%M:%S) 66 return time 67 68 # 获取当前时间转化字符串 69 70 classmethod 71 def test_report(cls): 72 nowtime = datetime.today() 73 reporti
11、me = nowtime.strftime(%Y%m%d%H%M%S) 74 reportname = reportime + .xml 75 return reportname 76 77 # 获取测试报告文献名称 78 79 classmethod 80 def test_main(cls): 81 global testresults 82 table = CreateExcel.open_excel() 83 nrows = CreateExcel.get_nrows(table) 84 xml = minidom.Document() 85 xml.appendChild(xml.c
12、reateComment(测试报告) 86 caselist = xml.createElement(caselist) 87 xml.appendChild(caselist) 88 for i in range(0,nrows - 1): 89 testid = CreateExcel.get_id(table,nrows)i 90 testname = CreateExcel.get_name(table,nrows)i 91 testdata = CreateExcel.get_data(table,nrows)i 92 testurl = CreateExcel.get_url(ta
13、ble,nrows)i 93 testmethod = CreateExcel.get_method(table,nrows)i 94 testpattern = CreateExcel.get_pattern(table,nrows)i 95 96 # 执行测试 97 CreateTest.test_on() 98 testresults = CreateTest.test_api(testmethod,testurl,testdata) 99 testcode = str(testresults.status_code)100 try:101 CreateTest.test_http(te
14、stresults.status_code)102 except AttributeError:103 pass104 CreateTest.test_close()105 # 执行结束106 # 生成xml文献107 case = xml.createElement(case)108 case.setAttribute(id,testid)109 # 输入用例ID110 111 name = xml.createElement(name)112 name.appendChild(xml.createTextNode(testname)113 # 输入用例名称114 method = xml.
15、createElement(method)115 method.appendChild(xml.createTextNode(testmethod)116 # 输入接口类型117 code = xml.createElement(code)118 code.appendChild(xml.createTextNode(testcode)119 # 输入用例返回状态码120 result = xml.createElement(result)121 result.appendChild(xml.createTextNode(CreateTest.test_result(testpattern)1
16、22 # 输入用例测试成果123 time = xml.createElement(time)124 time.appendChild(xml.createTextNode(CreateTest.test_time()125 # 输入用例执行时间126 127 case.appendChild(name)128 case.appendChild(method)129 case.appendChild(code)130 case.appendChild(result)131 case.appendChild(time)132 133 caselist.appendChild(case)134 #
17、 xml文献生成结束135 filename = file(CreateTest.test_report(),w+)136 # 生成以当前时间命名测试报告文献137 xml.writexml(filename)138 filename.close()139 # 关闭文献140 141 142 if _name_ = _main_:143 CreateTest.test_main()下面是测试入口: 1 #!/usr/bin/env python 2 # -*- coding:utf_8 -*- 3 # * 4 # interface.py 5 # Author :ChenLei 6 # Version :2.0 7 # Date :-4-15 8 # * 9 10 import time11 from createtest import CreateTest12 13 start = time.clock()14 CreateTest.test_main()15 end = time.clock()16 17 print 接口自动化脚本运营时间:%.03f seconds % (end - start)运营后自动生成 当前时间xml文献 如下:分类:python