Python+requests+unittest+excel实现接口自动化测试框架
Posted 旭旭杂货店
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python+requests+unittest+excel实现接口自动化测试框架相关的知识,希望对你有一定的参考价值。
一、框架结构:
工程目录
二、Case文件设计
三、基础包 base
3.1 封装get/post请求(runmethon.py)
1 import requests
2 import json
3 class RunMethod:
4 def post_main(self,url,data,header=None):
5 res = None
6 if header !=None:
7 res = requests.post(url=url,data=data,headers=header)
8 else:
9 res = requests.post(url=url,data=data)
10 return res.json()
11
12 def get_main(self,url,data=None,header=None):
13 res = None
14 if header !=None:
15 res = requests.get(url=url,data=data,headers=header,verify=False)
16 else:
17 res = requests.get(url=url,data=data,verify=False)
18 return res.json()
19
20 def run_main(self,method,url,data=None,header=None):
21 res = None
22 if method == \'Post\':
23 res = self.post_main(url,data,header)
24 else:
25 res = self.get_main(url,data,header)
26 return json.dumps(res,ensure_ascii=False,sort_keys=True,indent=2)
3.2 封装mock(mock.py)
1 from mock import mock
2 #模拟mock 封装
3 def mock_test(mock_method,request_data,url,method,response_data):
4 mock_method = mock.Mock(return_value=response_data)
5 res = mock_method(url,method,request_data)
6 return res
四、数据操作包 operation_data
4.1 获取excel单元格中的内容(get_data.py)
1 #coding:utf-8
2 from tool.operation_excel import OperationExcel
3 import data_config
4 from tool.operation_json import OperetionJson
5 from tool.connect_db import OperationMysql
6 class GetData:
7 def __init__(self):
8 self.opera_excel = OperationExcel()
9
10 #去获取excel行数,就是case的个数
11 def get_case_lines(self):
12 return self.opera_excel.get_lines()
13
14 #获取是否执行
15 def get_is_run(self,row):
16 flag = None
17 col = int(data_config.get_run())
18 run_model = self.opera_excel.get_cell_value(row,col)
19 if run_model == \'yes\':
20 flag = True
21 else:
22 flag = False
23 return flag
24
25 #是否携带header
26 def is_header(self,row):
27 col = int(data_config.get_header())
28 header = self.opera_excel.get_cell_value(row,col)
29 if header != \'\':
30 return header
31 else:
32 return None
33
34 #获取请求方式
35 def get_request_method(self,row):
36 col = int(data_config.get_run_way())
37 request_method = self.opera_excel.get_cell_value(row,col)
38 return request_method
39
40 #获取url
41 def get_request_url(self,row):
42 col = int(data_config.get_url())
43 url = self.opera_excel.get_cell_value(row,col)
44 return url
45
46 #获取请求数据
47 def get_request_data(self,row):
48 col = int(data_config.get_data())
49 data = self.opera_excel.get_cell_value(row,col)
50 if data == \'\':
51 return None
52 return data
53
54 #通过获取关键字拿到data数据
55 def get_data_for_json(self,row):
56 opera_json = OperetionJson()
57 request_data = opera_json.get_data(self.get_request_data(row))
58 return request_data
59
60 #获取预期结果
61 def get_expcet_data(self,row):
62 col = int(data_config.get_expect())
63 expect = self.opera_excel.get_cell_value(row,col)
64 if expect == \'\':
65 return None
66 return expect
67
68 #通过sql获取预期结果
69 def get_expcet_data_for_mysql(self,row):
70 op_mysql = OperationMysql()
71 sql = self.get_expcet_data(row)
72 res = op_mysql.search_one(sql)
73 return res.decode(\'unicode-escape\')
74
75 def write_result(self,row,value):
76 col = int(data_config.get_result())
77 self.opera_excel.write_value(row,col,value)
78
79 #获取依赖数据的key
80 def get_depend_key(self,row):
81 col = int(data_config.get_data_depend())
82 depent_key = self.opera_excel.get_cell_value(row,col)
83 if depent_key == "":
84 return None
85 else:
86 return depent_key
87
88 #判断是否有case依赖
89 def is_depend(self,row):
90 col = int(data_config.get_case_depend())
91 depend_case_id = self.opera_excel.get_cell_value(row,col)
92 if depend_case_id == "":
93 return None
94 else:
95