Пример #1
0
    def write_process_exc_log_database(self):
        '''
        写接口执行日志入数据库
        :param net_name: 网络环境名
        :param database: 指定数据库
        :return: 数据库写入结果
        '''

        sql = "INSERT INTO process_exc_log (project,task,process_tag,scene,process_status,is_exc,interface_tag,interface_exc_success_num,interface_exc_fail_num,interface_exc_time,exc_status,jump_process_tag,report_record,remark) VALUES (\'" + \
              self.process_log_data['project'] + "\',\'"+ self.process_log_data['task'] + "\',\'"+ self.process_log_data['process_tag'] + "\',\'"+ self.process_log_data['scene'] + "\',\'"+ str(self.process_log_data['process_status']) + "\',\'" \
               + str(self.process_log_data['is_exc']) + "\',\'"+ str(self.process_log_data['interface_tag']) + "\',\'"+ str(self.process_log_data['interface_exc_success_num']) + "\',\'"+ str(self.process_log_data['interface_exc_fail_num']) + "\',\'"+ str(self.process_log_data['interface_exc_time']) + "\',\'"+ str(self.process_log_data['exc_status']) + "\',\'"+ str(self.process_log_data['jump_process_tag']) + "\',\'"\
              + str(self.process_log_data['report_record']) + "\',\"" + str(interface_test.set_escape_character(self.process_log_data['remark'])) + "\")"

        sql_exc(sql)
Пример #2
0
    def run(self):
        '''执行'''

        #设置report_record
        self.set_begin_time()

        #预处理process_base_log日志
        self.set_process_base_log()

        #判断流程列表格式,必须是list,tuple
        if type(self.process_list) not in (list,tuple):
            self.process_base_log['process_end_time'] = get_time_stamp()
            self.process_base_log['create_time'] = get_time_stamp()
            self.process_base_log['message'] = 'Error! 流程设置的数据类型应为list, 流程为: ' + str(self.process_list) + '; 数据类型为: ' +'\\\''.join(str(type(self.process_list)).split('\''))
            self.write_process_base_log_database()
            return False

        #判断前置设置全局数据字典状态,设置为0时,流程整体异常退出
        if self.global_Preset == 0:
            self.process_base_log['process_end_time'] = get_time_stamp()
            self.process_base_log['create_time'] = get_time_stamp()
            self.write_process_base_log_database()
            return False

        #获取按顺序执行的所有流程的起始process_tag
        begin_process_list = self.get_begin_process_list()


        if begin_process_list == []:
            self.process_base_log['process_end_time'] = get_time_stamp()
            self.process_base_log['create_time'] = get_time_stamp()
            self.process_base_log['message'] = 'Warning! 未能获取流程执行起始process_tag,  获取的begin_process_list为: ' + interface_test.set_escape_character(str(self.process_list))
            self.write_process_base_log_database()
            return False

        for n in begin_process_list:
            process_tag = n
            while(process_tag != 'end'):
                #获取单个流程数据
                process_record_row = self.get_process_record_row_data(process_tag)
                #流程数据转为dict格式, 若获取流程数据失败则退出当前流程
                if process_record_row['data'] == ():
                    self.process_base_log['process_end_time'] = get_time_stamp()
                    self.process_base_log['create_time'] = get_time_stamp()
                    self.process_base_log['message'] = 'Error! 获取流程数据失败,退出当前流程: ' + str(process_tag) + ', 起始流程为: ' + str(n)
                    self.write_process_base_log_database()
                    break
                self.row_to_process_data(process_record_row['data'][0])

                #构建流程日志
                self.create_process_exc_log()

                #判断当前流程是否可执行,不可执行退出当前流程
                if self.process_data['is_exc'] == 0:
                    self.process_log_data['remark'] += 'Error! 当前流程已设置为不可执行'
                    self.write_process_exc_log_database()
                    break

                #获取流程接口数据
                interface_sql_row = interface_test.get_interface_row_data(self.process_data['interface_tag'])
                if interface_sql_row is False:
                    self.process_log_data['remark'] += 'Error! 获取接口数据失败:' + str(self.process_data['interface_tag'])
                    self.write_process_exc_log_database()
                    break
                else:
                    if interface_sql_row['row_num'] == 0:
                        self.process_log_data['remark'] += 'Error! 未查询到指定接口数据:' + str(self.process_data['interface_tag'])
                        self.write_process_exc_log_database()
                        break
                    #接口数据转为dict格式
                    self.sql_row_result = interface_test.row_to_interface_data(self, interface_sql_row['data'][0])
                    #检查点替换
                    self.change_checkpoint()
                    #headers替换
                    self.set_headers()


                    #入参替换
                    if self.process_data['input_parameter'] != '':
                        inputparameter_dict = self.inputparameter_to_dict()
                        if self.interface_data['method'].lower() == 'get':
                            self.replace_get_url(inputparameter_dict)
                        elif self.interface_data['method'].lower() == 'post':
                            self.replace_post_body(inputparameter_dict)

                    #判断连续执行次数,最大失败次数。当接口连续多次执行时,当达到设置执行次数或达到最大执行次数时退出
                    max_exc_num = self.process_data['max_exc_num']
                    max_fail_exc_num = self.process_data['max_fail_exc_num']
                    success_num = 0
                    fail_num = 0
                    while(max_exc_num):
                        #接口请求执行
                        interface_test.interface_exc(self)
                        # 写接口执行日志入数据库
                        interface_test.write_interface_exc_log_database(self)
                        if self.log_data['status_code'] == 200 and self.log_data['check_status'] == 1:
                            success_num += 1
                            max_exc_num -= 1
                            if success_num + fail_num == self.process_data['max_exc_num']:
                                break
                        else:
                            fail_num += 1
                            if fail_num == max_fail_exc_num:
                                break


                    self.process_log_data['interface_exc_success_num'] = success_num
                    self.process_log_data['interface_exc_fail_num'] = fail_num
                    self.process_log_data['interface_exc_time'] = self.log_data['exc_time']

                    # 判断接口执行状态码和检查状态,提取出参和cookie设置,流程跳转判断
                    if self.log_data['status_code'] == 200 and self.log_data['check_status'] == 1:

                        #提取出参
                        if self.process_data['output_parameter'] != '':
                            self.outputparameter_to_dict()

                        #cookie设置
                        if self.process_data['cookies_set'] == 1:
                            self.global_dict['cookies'] = self.log_data['cookies']
                        elif self.process_data['cookies_set'] == 2:
                            self.global_dict['cookies'].update(self.log_data['cookies'])
                        elif self.process_data['cookies_set'] == 3:
                            self.global_dict['cookies'] = {}

                        process_tag = self.process_data['success_jump']
                        self.process_log_data['exc_status'] = 1
                        self.process_log_data['jump_process_tag'] = self.process_data['success_jump']
                    else:
                        process_tag = self.process_data['fail_jump']
                        self.process_log_data['exc_status'] = 0
                        self.process_log_data['jump_process_tag'] = self.process_data['fail_jump']
                        self.process_log_data['remark'] += 'Error! 流程执行失败' + str(self.process_data['process_tag'])

                    # print(self.global_dict)
                    self.write_process_exc_log_database()


        self.process_base_log['process_end_time'] = get_time_stamp()
        self.process_base_log['create_time'] = get_time_stamp()
        self.process_base_log['message'] = 'Finish! 流程执行完成!'
        self.write_process_base_log_database()
Пример #3
0
    def write_process_base_log_database(self):
        '''
        写接口执行日志入数据库
        :param net_name: 网络环境名
        :param database: 指定数据库
        :return: 数据库写入结果
        '''

        sql = "INSERT INTO process_base_log (report_record,project,task,process_list,create_time,process_begin_time,process_end_time,message) VALUES (\'" + \
              self.process_base_log['report_record'] + "\',\'"+ self.process_base_log['project'] + "\',\'"+ self.process_base_log['task'] + "\',\""+ str(interface_test.set_escape_character(self.process_base_log['process_list'])) + "\",\'"+ str(self.process_base_log['create_time']) + "\',\'" \
               + str(self.process_base_log['process_begin_time']) + "\',\'"+ str(self.process_base_log['process_end_time']) + "\',\""+ str(interface_test.set_escape_character(self.process_base_log['message'])) + "\")"

        sql_exc(sql)