标签:[] 名称 pytho err efault title 类型 tar int
代码如下:
1 #!/usr/bin/env python 2 # -*- coding: utf_8 -*- 3 # 获取测试用例文件excel 4 5 import xlrd 6 import json 7 8 9 class CreateExcel: 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 # 获取行号 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 testname 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 def 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 testmethod 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 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, url, 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 41 def test_close(cls): 42 print "用例执行结束" 43 44 @classmethod 45 def test_result(cls, pa): 46 global report 47 try: 48 pattern = re.compile(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 @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 reportime = 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.createComment("测试报告")) 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(table, 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(testresults.status_code) 102 except AttributeError: 103 pass 104 CreateTest.test_close() 105 # 执行结束 106 # 生成xml文件 107 case = xml.createElement("case") 108 case.setAttribute("id", testid) 109 # 输入用例ID 110 111 name = xml.createElement("name") 112 name.appendChild(xml.createTextNode(testname)) 113 # 输入用例名称 114 method = xml.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))) 122 # 输入用例测试结果 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 # 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 : 2016-4-15 8 # **************************************************************** 9 10 import time 11 from createtest import CreateTest 12 13 start = time.clock() 14 CreateTest.test_main() 15 end = time.clock() 16 17 print "接口自动化脚本运行时间:%.03f seconds" % (end - start)
运行后自动生成 当前时间的xml文件 如下:
标签:[] 名称 pytho err efault title 类型 tar int
原文地址:http://www.cnblogs.com/tester-l/p/6064227.html