Beispiel #1
0
class CompareParam(object):
    def __init__(self, params_interface):
        """
        初始化数据
        :param params_interface: 接口请求参数
        """
        self.params_interface = params_interface  # 接口入参
        self.id_case = params_interface.get('id')  # 测试用例ID
        self.result_list_response = []  # 定义用来存储 参数集的空列表
        self.params_to_compare = params_interface.get(
            'params_to_compare')  # 定义参数完整性的预期结果

        self.db = OperationDbInterface()

    def compare_code(self, result_interface):
        """

        :param result_interface: HTTP返回数据包
        :return:  ,返回信息message,数据data
        """
        try:
            if isinstance(result_interface,
                          str) and result_interface.startswith('{'):
                temp_result_interface = json.loads(
                    result_interface)  # 将字符类型转换为字典类型
                temp_code_to_compare = self.params_interface.get(
                    'code_to_compare')
                if temp_code_to_compare in temp_result_interface.keys():
                    if str(temp_result_interface.get(
                            temp_code_to_compare)) == str(
                                self.params_interface.get('code_expect')):
                        result = {
                            'code': '0000',
                            'message': '关键字参数值相同',
                            'data': []
                        }
                        print(
                            "############",
                            "UPDATE case_interface set code_actual=%s,"
                            "result_code_compare =%s where id =%s" % (str(
                                temp_result_interface.get(temp_code_to_compare)
                            ).replace("'", "\\'"), 1, self.id_case))
                        self.db.op_sql(
                            "UPDATE case_interface set code_actual=%s,"
                            "result_code_compare =%s where id =%s" % (str(
                                temp_result_interface.get(temp_code_to_compare)
                            ).replace("'", "\\'"), 1, self.id_case))
                    elif str(temp_result_interface.get(
                            temp_code_to_compare)) != str(
                                self.params_interface.get('code_expect')):
                        result = {
                            'code': '1003',
                            'message': '关键字参数值不相同',
                            'data': []
                        }
                        print(
                            "############",
                            "UPDATE case_interface set code_actual='%s',"
                            "result_code_compare =%s where id =%s" % (str(
                                temp_result_interface.get(temp_code_to_compare)
                            ).replace("'", "\\'"), 0, self.id_case))
                        self.db.op_sql(
                            "UPDATE case_interface set code_actual='%s',"
                            "result_code_compare =%s where id =%s" % (str(
                                temp_result_interface.get(temp_code_to_compare)
                            ).replace("'", "\\'"), 0, self.id_case))
                    else:
                        result = {
                            'code': '1002',
                            'message': '关键字参数值比较出错',
                            'data': []
                        }
                        self.db.op_sql(
                            "UPDATE "
                            " set code_actual='%s',"
                            "result_code_compare =%s where id =%s" %
                            (temp_result_interface.get(temp_code_to_compare),
                             3, self.id_case))
                else:
                    result = {
                        'code': '1001',
                        'message': '返回包数据无关键字参数',
                        'data': []
                    }
                    self.db.op_sql(
                        "UPDATE case_interface set result_code_compare =%s where id =%s"
                        % (2, self.id_case))
            else:
                result = {'code': '1000', 'message': '返回包格式不合法', 'data': []}
                self.db.op_sql(
                    "UPDATE case_interface set result_code_compare =%s where id =%s"
                    % (4, self.id_case))

        except Exception as e:
            result = {'code': '9999', 'message': '关键字参数值比较异常', 'data': []}
            self.db.op_sql(
                "UPDATE case_interface set result_code_compare =%s where id =%s"
                % (9, self.id_case))

            MyLog.error(e)
        finally:
            return result

    def get_compare_params(self, result_interface):
        """

        :param result_interface: HTTP返回包数据
        :return: 返回码code,返回信息message,数据data
        """
        try:
            if result_interface.startswith('{') and isinstance(
                    result_interface, str):
                temp_result_interface = json.loads(
                    result_interface)  # 将字符串类型转换为字典类型
                self.result_list_response = temp_result_interface.keys()
                result = {
                    'code': '0000',
                    'message': '成功',
                    'data': self.result_list_response
                }
            else:
                result = {
                    'code': '1000',
                    'message': '返回包格式不合法',
                    'data': self.result_list_response
                }

        except Exception as e:
            result = {'code': '9999', 'message': '处理数据异常', 'data': []}
            MyLog.error(e)
        finally:
            return result

    def compare_params_complete(self, result_interface):
        """
        定义参数完整性的比较方法,将传参值与__recur_params 方法返回结果进行比较
        :param result_interface:
        :return: 返回码Code,返回信息Message,数据data
        """
        try:
            temp_compare_params = self.__recur_params__(
                result_interface)  # 获取返回包的参数集
            if temp_compare_params.get('code') == '0000':
                temp_result_list_response = temp_compare_params.get(
                    'data')  # 获取接口返回参数去重列表

                if self.params_to_compare.startswith('[') and isinstance(
                        self.params_to_compare, str):  # 判断测试用例列表中预期结果集是否为列表
                    print('debug', self.params_to_compare)
                    list_params_to_compare = eval(
                        self.params_to_compare)  # 将数据库中的unicode编码数据转换为列表

                    if set(list_params_to_compare).issubset(
                            set(temp_result_list_response)):  # 判断集合包含关系
                        result = {
                            'code': '0000',
                            'message': '参数完整性比较一致',
                            'data': []
                        }
                        self.db.op_sql(
                            "UPDATE case_interface set params_actual=\"%s\","
                            "result_params_compare =%s where id =%s" %
                            (temp_result_list_response, 1, self.id_case))
                    else:
                        result = {
                            'code': '3001',
                            'message': '实际结果中元素不都在预期结果中',
                            'data': []
                        }
                        self.db.op_sql(
                            "UPDATE case_interface set params_actual=\"%s\","
                            "result_params_compare =%s where id =%s" %
                            (temp_result_list_response, 0, self.id_case))
                else:
                    result = {
                        'code': '4001',
                        'message': '用例中待比较参数集错误',
                        'data': self.params_to_compare
                    }
            else:
                result = {
                    'code': '2001',
                    'message': '调用__recur_params__方法返回错误',
                    'data': self.params_to_compare
                }
                self.db.op_sql(
                    "UPDATE case_interface set result_params_compare =%s where id =%s"
                    % (2, self.id_case))
        except Exception as e:
            result = {'code': '9999', 'message': '参数完整性比较异常', 'data': []}
            self.db.op_sql(
                "UPDATE case_interface set result_params_compare =%s where id =%s"
                % (9, self.id_case))
            MyLog.error(e)
        return result

    def __recur_params__(self, result_interface):
        """
        定义递归操作,将接口返回数据中的参数名写入列表中(去重)
        :return:
        """
        try:
            if isinstance(result_interface,
                          str) and result_interface.startswith('{'):
                temp_result_interface = json.loads(result_interface)
                self.__recur_params__(temp_result_interface)
            elif isinstance(result_interface, dict):  # 入参是字典

                for param, value in result_interface.items():
                    self.result_list_response.append(param)
                    if isinstance(value, list):
                        for param in value:
                            self.__recur_params__(param)
                    elif isinstance(value, dict):
                        self.__recur_params__(value)
                    else:
                        continue
            else:
                pass
        except Exception as e:
            MyLog.error(e)
            result = {'code': '9999', 'message': '数据处理异常', 'data': []}
            return result
        return {
            'code': '0000',
            'message': '成功',
            'data': list(set(self.result_list_response))
        }
Beispiel #2
0
class Run(object):
    def __init__(self):
        self.db = OperationDbInterface()
        self.cases_to_run = []
        self.cache = Cache(maxsize=256, ttl=0, timer=time.time)

    def get_all_cases(self):
        """
        获取所有测试用例
        :return: []
        """
        sql = 'select * from case_interface'
        print(self.db.select_all(sql))

    def get_one_case(self, condition):
        """
        获取一条测试用例
        :return: []
        """

    def get_cases_for_run(self):
        """
        获取要执行的测试用例
        :return:
        """
        pass

    def execute_test_case(self, *arg):
        """
        执行测试用例
        :param arg:
        :return:
        """
        test_interface = RequestInterface()
        sql = "SELECT * FROM `case_interface` WHERE case_status = 1"
        result = self.db.select_all(sql)
        if result.get('code') == '0000' and result.get('data'):
            MyLog.debug('获取执行接口成功')
            datas = result.get('data')
            for temp_case_interface in datas:
                obj = extraDB(temp_case_interface, self.cache)
                params_interface = obj.replace()  # 执行测试前重构测试数据
                url_interface = params_interface.get('url_interface')
                id_case = temp_case_interface.get('id')
                name_interface = temp_case_interface.get('name_interface')
                name_case = temp_case_interface.get('name_case')
                headdata = ast.literal_eval(params_interface.get('header_interface'))
                type_interface = params_interface.get('exe_mode')
                if url_interface != '' and headdata != '' and type_interface != '':
                    temp_level_check = temp_case_interface.get('check_level')  # 检查级别

                    result_http_respones = test_interface.http_request(url_interface, headdata,
                                                                       params_interface.get('params_interface'),
                                                                       type_interface)
                    print(result_http_respones)
                    MyLog.debug("用例返回消息:{result}".format(result=result_http_respones.get('data')))
                    obj.setvar(result_http_respones.get('data'))
                    self.db.op_sql("UPDATE case_interface  SET result_interface = '%s' where id = %s " %
                                   (result_http_respones.get('data'), id_case))  # 将返回包数据写入用例表

                    if result_http_respones['code'] == '0000' and len(result_http_respones['data']) != 0:
                        base_compare = CompareParam(temp_case_interface)
                        if '0' in list(temp_level_check):  # 执行关键值参数检查
                            result_compare_code = base_compare.compare_code(result_http_respones.get('data'))
                            MyLog.debug('用例编号:%s|检查级别:关键字参数值|接口名称:%s|用例名称:%s|提示信息:%s \n'
                                        % (id_case, name_interface, name_case, result_compare_code['message']))
                        if '1' in list(temp_level_check):  # 执行参数完整性检查
                            result_compare_params_complete = base_compare.compare_params_complete(
                                result_http_respones.get('data'))
                            MyLog.debug('用例编号:%s|检查级别:参数完整性|接口名称:%s|用例名称:%s|提示信息:%s \n'
                                        % (id_case, name_interface, name_case, result_compare_params_complete['message']))
                    elif len(result_http_respones['data']) == 0:
                        MyLog.debug('接口名称: %s|信息错误:获取用例数据为空,请检查用例\n' % name_interface)
                    else:
                        MyLog.debug('接口名称: %s|信息错误:获取用例数据失败' % name_interface)
Beispiel #3
0
                                        'exe_mode')  # 执行环境
                                    param_interface = temp_case_interface.get(
                                        'params_interface')  # 接口请求参数
                                    print('param_interface', param_interface)
                                    result_http_respones = base_request.http_request(
                                        interface_url=url_interface,
                                        headerdata=headerdata,
                                        interface_parm=param_interface,
                                        request_type=type_interface)
                                    # 发送http请求
                                    print('接口地址', url_interface, '\n', '请求参数',
                                          param_interface, '\n'
                                          '返回包数据', result_http_respones)

                                    base_db.op_sql(
                                        "UPDATE case_interface  SET result_interface = '%s' where id = %s "
                                        % (result_http_respones.get('data'),
                                           id_case))  # 将返回包数据写入用例表

                                    if result_http_respones[
                                            'code'] == '0000' and len(
                                                result_http_respones['data']
                                            ) != 0:
                                        for child_level_check in temp_level_check:  # 循环检查级别
                                            base_compare = CompareParam(
                                                temp_case_interface)
                                            if child_level_check in (
                                                    0, ):  # 执行关键值参数检查
                                                result_compare_code = base_compare.compare_code(
                                                    result_http_respones.get(
                                                        'data'))
                                                print(