.
优质范文
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 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(