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             return depend_case_id
 96 
 97     #获取数据依赖字段
 98     def get_depend_field(self,row):
 99         col = int(data_config.get_field_depend())
100         data = self.opera_excel.get_cell_value(row,col)
101         if data == "":
102             return None
103         else:
104             return data
复制代码

4.2 获取excel中每个列(data_config.py)

复制代码
 1 #coding:utf-8
 2 class global_var:
 3     #case_id
 4     Id = \'0\'
 5     request_name = \'1\'
 6     url = \'2\'
 7     run = \'3\'
 8     request_way = \'4\'
 9     header = \'5\'
10     case_depend = \'6\'
11     data_depend = \'7\'
12     field_depend = \'8\'
13     data = \'9\'
14     expect = \'10\'
15     result = \'11\'
16 #获取caseid
17 def get_id():
18     return global_var.Id
19 
20 #获取url
21 def get_url():
22     return global_var.url
23 
24 def get_run():
25     return global_var.run
26 
27 def get_run_way():
28     return global_var.request_way
29 
30 def get_header():
31     return global_var.header
32 
33 def get_case_depend():
34     return global_var.case_depend
35 
36 def get_data_depend():
37     return global_var.data_depend
38 
39 def get_field_depend():
40     return global_var.field_depend
41 
42 def get_data():
43     return global_var.data
44 
45 def get_expect():
46     return global_var.expect
47 
48 def get_result():
49     return global_var.result
50 
51 def get_header_value():
52     return global_var.header
复制代码

4.3 解决数据依赖(dependent.py )

复制代码
 1 #coding:utf-8
 2 import sys
 3 import json
 4 sys.path.append(\'C:/Users/lxz/Desktop/InterFace_JIA\')
 5 from tool.operation_excel import OperationExcel
 6 from base.runmethod import RunMethod
 7 from operation_data.get_data import GetData
 8 from jsonpath_rw import jsonpath,parse
 9 class DependdentData:
10     def __init__(self,case_id):
11         self.case_id = case_id
12         self.opera_excel = OperationExcel()
13         self.data = GetData()
14 
15     #通过case_id去获取该case_id的整行数据
16     def get_case_line_data(self):
17         rows_data = self.opera_excel.get_rows_data(self.case_id)
18         return rows_data
19 
20     #执行依赖测试,获取结果
21     def run_dependent(self):
22         run_method = RunMethod()
23         row_num  = self.opera_excel.get_row_num(self.case_id)
24         request_data = self.data.get_data_for_json(row_num)
25         #header = self.data.is_header(row_num)
26         method = self.data.get_request_method(row_num)
27         url = self.data.get_request_url(row_num)
28         res = run_method.run_main(method,url,request_data)
29         return json.loads(res)
30 
31     #根据依赖的key去获取执行依赖测试case的响应,然后返回
32     def get_data_for_key(self,row):
33         depend_data = self.data.get_depend_key(row)
34         response_data = self.run_dependent()
35         json_exe = parse(depend_data)
36         madle = json_exe.find(response_data)
37         return [math.value for math in madle][0]
38 
39 if __name__ == \'__main__\':
40     order = {
41         "data": {
42             "_input_charset": "utf-8", 
43             "body": "京东订单-1710141907182334", 
44             "it_b_pay": "1d", 
45             "notify_url": "http://order.imooc.com/pay/notifyalipay", 
46             "out_trade_no": "1710141907182334", 
47             "partner": "2088002966755334", 
48             "payment_type": "1", 
49             "seller_id": "yangyan01@tcl.com", 
50             "service": "mobile.securitypay.pay", 
51             "sign": "kZBV53KuiUf5HIrVLBCcBpWDg%2FnzO%2BtyEnBqgVYwwBtDU66Xk8VQUTbVOqDjrNymCupkVhlI%2BkFZq1jOr8C554KsZ7Gk7orC9dDbQl                        pr%2BaMmdjO30JBgjqjj4mmM%2Flphy9Xwr0Xrv46uSkDKdlQqLDdGAOP7YwOM2dSLyUQX%2Bo4%3D", 
52             "sign_type": "RSA", 
53             "string": "_input_charset=utf-8&body=京东订单-1710141907182334&it_b_pay=1d&notify_url=http://order.imooc.com/pay/                          notifyalipay&out_trade_no=1710141907182334&partner=2088002966755334&payment_type=1&seller_id=yangyan01@                          tcl.com&service=mobile.securitypay.pay&subject=京东订单-1710141907182334&total_fee=299&sign=kZBV53KuiUf5H                          IrVLBCcBpWDg%2FnzO%2BtyEnBqgVYwwBtDU66Xk8VQUTbVOqDjrNymCupkVhlI%2BkFZq1jOr8C554KsZ7Gk7orC9dDbQlpr%2BaMmdjO30                          JBgjqjj4mmM%2Flphy9Xwr0Xrv46uSkDKdlQqLDdGAOP7YwOM2dSLyUQX%2Bo4%3D&sign_type=RSA", 
54             "subject": "京东订单-1710141907182334", 
55             "total_fee": 299
56             }, 
57             "errorCode": 1000, 
58             "errorDesc": "成功", 
59             "status": 1, 
60             "timestamp": 1507979239100
61         }
62     res = "data.out_trade_no"
63     json_exe = parse(res)
64     madle = json_exe.find(order)
65     print [math.value for math in madle][0]
复制代码

五、工具类包 tool

5.1 操作excel (operation_excel.py)

复制代码
 1 #coding:utf-8
 2 import xlrd
 3 from xlutils.copy import copy
 4 class OperationExcel:
 5     def __init__(self,file_name=None,sheet_id=None):
 6         if file_name:
 7             self.file_name = file_name
 8             self.sheet_id = sheet_id    
 9         else:
10             self.file_name = \'../dataconfig/case1.xls\'
11             self.sheet_id = 0
12         self.data = self.get_data()
13 
14     #获取sheets的内容
15     def get_data(self):
16         data = xlrd.open_workbook(self.file_name)
17         tables = data.sheets()[self.sheet_id]
18         return tables
19 
20     #获取单元格的行数
21     def get_lines(self):
22         tables = self.data
23         return tables.nrows
24 
25     #获取某一个单元格的内容
26     def get_cell_value(self,row,col):
27         return self.data.cell_value(row,col)
28 
29     #写入数据
30     def write_value(self,row,col,value):
31         \'\'\'
32         写入excel数据
33         row,col,value
34         \'\'\'
35         read_data = xlrd.open_workbook(self.file_name)
36         write_data = copy(read_data)
37         sheet_data = write_data.get_sheet(0)
38         sheet_data.write(row,col,value)
39         write_data.save(self.file_name)
40 
41     #根据对应的caseid 找到对应行的内容
42     def get_rows_data(self,case_id):
43         row_num = self.get_row_num(case_id)
44         rows_data = self.get_row_values(row_num)
45         return rows_data
46 
47     #根据对应的caseid找到对应的行号
48     def get_row_num(self,case_id):
49         num = 0
50         clols_data = self.get_cols_data()
51         for col_data in clols_data:
52             if case_id in col_data:
53                 return num
54             num = num+1
55 
56 
57     #根据行号,找到该行的内容
58     def get_row_values(self,row):
59         tables = self.data
60         row_data = tables.row_values(row)
61         return row_data
62 
63     #获取某一列的内容
64     def get_cols_data(self,col_id=None):
65         if col_id != None:
66             cols = self.data.col_values(col_id)
67         else:
68             cols = self.data.col_values(0)
69         return cols
70 
71 
72 if __name__ == \'__main__\':
73     opers = OperationExcel()
74     print opers.get_cell_value(1,2)
复制代码

5.2判断字符串包含,判断字典是否相等(common_util.py)

复制代码
 1 #coding:utf-8
 2 import json
 3 class CommonUtil:
 4     def is_contain(self,str_one,str_two):
 5         \'\'\'
 6         判断一个字符串是否再另外一个字符串中
 7         str_one:查找的字符串
 8         str_two:被查找的字符串
 9         \'\'\'
10         flag = None
11         if isinstance(str_one,unicode):
12             str_one = str_one.encode(\'unicode-escape\').decode(\'string_escape\')
13         return cmp(str_one,str_two)
14         if str_one in str_two:
15             flag = True
16         else:
17             flag = False
18         return flag
19 
20 
21     def is_equal_dict(self,dict_one,dict_two):
22         \'\'\'
23         判断两个字典是否相等
24         \'\'\'
25         if isinstance(dict_one,str):
26             dict_one = json.loads(dict_one)
27         if isinstance(dict_two,str):
28             dict_two = json.loads(dict_two)
29         return cmp(dict_one,dict_two)
复制代码

5.3 操作header(operation_herder.py)