解决自动化接口测试的数据依赖

本篇博客是延续上一篇博客(https://blog.csdn.net/guozhiguo86/article/details/100090828)把其中涉及到的怎样解决数据依赖的问题,单独拎出来做讲解。

模板释义

在excel模板中涉及到数据依赖的列如下图所示:

如上图所示,一共有:rely_case_id、rely_data、request_rely_file及request_data 这四列。

  1. rely_case_id:表示当前case所依赖的数据所在的case id
  2. rely_data:表示当前case所依赖的case的数据所对应的响应结果的字段(可能一些同学不明白,为什么这里不是直接写依赖的数据,因为接口所依赖的数据往往都是动态。至少我做自动化测试的这几年,没见过一次是静态数据依赖,如果是静态的,就不会让这么多的同学烦恼怎样解决)
  3. request_rely_file:请求数据中要替换的数据所对应的字段值
  4. request_data:请求数据key(需要说一下,该列保存的都是key。所有的请求数据都保存在json文件中。通过key来获取对应的具体的请求参数数据值)

解决思路

  1. 通过rely_case_id的值,来执行所依赖的case,并获取相应的响应结果
  2. 将获取的响应结果转为json/dict
  3. 通过jsonpath_rw 来截取rely_data 所对应的具体数据,并返回
  4. 当获取到所依赖的数据后,此时获取具体的请求数据
  5. 再将请求数据要替换的数据,替换为所依赖的数据即可
  6. 此时正常执行该用例即可。

具体代码

import sys
 
sys.path.append("../../")
from script.case.opera_api_case_excel import OperaApiCaseData
from script.method.run_method import RunMethod
from script.case.get_api_case_excel_data import GetCaseExcelData
from jsonpath_rw import parse
import json
 
import traceback
 
 
class RelyData():
    """
    解决case间的数据依赖  \n
    1:获取所依赖case所在行的所有数据  \n
    2:运行所依赖的case,并返回该case的请求结果  \n
    3:从请求的结果中获取所依赖的字段的值(使用jsonpath-rw)  \n
    """
 
    def __init__(self, rely_case_id, excel_path, sheet_name):
        """
        传入所依赖的case id 以及excel表的路径和sheet表的名称  \n
        :param rely_case_id: 所依赖的case id
        """
        self.case_id = rely_case_id
        self.excel_path = excel_path
        self.sheet_name = sheet_name
        self.opera_excel = OperaApiCaseData(self.excel_path, self.sheet_name)
        self.run_method = RunMethod()
        self.excel_data = GetCaseExcelData(self.excel_path, self.sheet_name)
 
    # def get_rely_case_row_data(self):
    #     """
    #     获取依赖的case所在行的数据  \n
    #     :return: 返回所依赖的case 所在的行的数据 List类型
    #     """
    #     row_data = self.opera_excel.get_case_row_data(self.case_id)
    #     return row_data
 
    def run_rely_case(self,json_file_path):
        """
        执行所依赖的case,并将运行的结果返回  \n
        :param json_file_path: request请求对应的json文件路径,如'../../data/request/105_web_login.json'
        :return: 返回执行所依赖的case的运行结果
        """
 
        run_method = RunMethod()
        row_num = self.opera_excel.get_rely_case_row_num(self.case_id)
        print("所依赖的case id 为:",self.case_id)
        print("所依赖的case 所在的行号为:",row_num)
        request_data = self.excel_data.get_request_data(row_num,json_file_path)
        print("run_rely_case  request_data",request_data)
        header = self.excel_data.get_is_header(row_num)
        url = self.excel_data.get_url(row_num)
        method = self.excel_data.get_method(row_num)
        res = run_method.run_main(method, url, request_data, header)
        print('run_rely_case res',res)
        # 将响应的结果json处理为str 并返回
        return json.dumps(res.json(), ensure_ascii=False, sort_keys=True, indent=2)
 
    def get_rely_data(self, row,json_file_path):
        """
        执行所依赖的case,并从返回的响应数据中获取目标数据  \n
        # response_data = {                         \n
        #   "reason": "成功",                        \n
        #   "status": 1                             \n
        # }                                          \n
        # rely_data = "staus"                       \n
        response_data = {                           \n
          "reason": "成功",                          \n
          "status": {"username":"123456",            \n
                     "pass":"12345stwetqw6"}         \n
        }                                            \n
        rely_data = "status.pass"                    \n
        如上面所示,想要查找status下的pass的值,在excel表中的rely_data 就应该写为status.pass  \n
        如果想要获取status的值,就应该为 status           \n
        :param row: 有依赖case的行号
        :param json_file_path: request 请求所对应的json路径,含文件名,如'../../data/request/XXXX.json'
        :return: 返回查找到的数据
        """
        # 获取 rely data 单元格的值
        rely_data = self.excel_data.get_rely_data(row)
        # 判断是否为空
        if rely_data == None or rely_data == "":
            return None
        else:
            try:
                # 获取所依赖的case的返回结果
                response_data = self.run_rely_case(json_file_path)
 
                # 把返回回来的字符串转化为json/dict 类型
                response_data = json.loads(response_data)
 
                jsonpath_expr = parse(rely_data)
                find_data = jsonpath_expr.find(response_data)
                return ([match.value for match in find_data][0])
            except Exception as msg:
                # print(msg)
                # print((traceback.print_exc()))
 
                # 输出完整的异常错误信息
                print('traceback.format_exc():', traceback.format_exc())
                return None
 if rely_case_id != None:
        self.rely_data = RelyData(rely_case_id, self.excel_path, self.sheet_name)
        # 执行所依赖的case,并获取所依赖的响应数据
        rely_response_data = self.rely_data.get_rely_data(i,json_file_path=self.sheet_req_json_file)
        # print(rely_response_data)
        # 获取依赖的字段
        rely_file = self.excel_wb.get_request_rely_file(i)
        # 更新请求数据
        param_data[rely_file] = rely_response_data
        # print(param_data)

注: 这上面的代码不支持多层依赖,嵌套依赖。

下一章节将就讲解 怎样解决多层依赖和嵌套依赖(需要用到递归)。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章