示例#1
0
class RunTest(GetData):
    # 实例化前完成所有的请求数据依赖处理
    @staticmethod
    def write_depend_data():
        options = input('是否需要执行用例依赖数据写入操作,确认执行请输入yes,不执行请输入其他任意字符:\n')
        if options == 'yes':
            write_excle()
            print('依赖数据写入完成')
        else:
            print("跳过写入依赖数据,开始执行测试")

    def __init__(self):
        super().__init__()
        self.data = GetData()
        self.op_testReport = Write_testReport_excle()
        # '''读取失败重试配置'''
        self.Config_Try_Num = self.yaml['Config_Try_Num']

    # 程序执行
    def go_on_run(self):
        global pass_count, fail_count
        pass_count = []
        fail_count = []
        rows_count = self.data.get_case_line()
        self.log.info(self.mylog.out_varname(rows_count))
        for i in range(2, rows_count + 1):
            # for i in range(19,19+1):
            is_run = self.data.get_is_run(i)
            if is_run is True:
                # ''' 处理请求'''
                response = self.data.request_info(i)
                expect = self.data.expectData(i)  # 断言,也就i是预期结果
                # '''处理断言'''
                self.assert_control(i, expect, response)
                self.log.info(self.mylog.out_varname(expect))
                self.log.info(self.mylog.out_varname(response))
            # else:
            #     contine_info='当前用例为第{}条,跳过,is_run={}'.format(i-1,is_run)
            #     self.log.info(self.mylog.out_varname(contine_info)

    #处理断言
    def assert_control(self, row, expect, response):
        expect_flag = self.Determine_assert_type(response)
        if expect_flag == 'str':
            # 调用srt判断方法处理断言
            __assert = self.com_assert.is_contain(expect, response)
        else:
            # str断言处理
            # 将expect转换为dict
            if not isinstance(expect, dict):
                # assert_pyobject:将断言转换为dict,并将true/false/null,转换为python对象
                expect = self.requestDataDispose.assert_pyobject(expect)
                __assert = self.com_assert.is_equal_dict(expect, response)
            else:
                # 针对sql断言特殊处理
                res_dict = self.dict_assert_res(expect, response)
                # print(expect)
                # print(res_dict)
                __assert = self.com_assert.is_equal_dict_sql_except(
                    expect, res_dict)
                # print(__assert)

        self.assert_result_write_excle(row, __assert)

    # 根据sql执行结果生成对应的response数据,用于断言判断
    def dict_assert_res(self, expect, response):
        if isinstance(expect, dict) and isinstance(response, dict):
            json_path = self.requestDataDispose.denpendKeyGenerate(
                str_in=expect, join_str=self.yaml['recursive_joinstr'])
            res_dict = {}
            for __path in json_path:
                res_result = self.requestDataDispose.depend_data_parse(
                    __path, response, index='all')
                if isinstance(res_result, dict) and res_result:
                    res_dict.update(res_result)
            return res_dict
        return False

    # 将断言结果写入excle
    def assert_result_write_excle(self, row, __assert):
        # 判断失败次数是否小于等于配置失败重试次数
        errorNum = 0
        while errorNum <= self.Config_Try_Num:
            if __assert:
                self.data.write_result(row, 'pass')
                self.log.info('测试通过')
                pass_count.append(row)
                self.log.info(self.mylog.out_varname(pass_count))
                errorNum = 0
                self.log.info(self.mylog.out_varname(errorNum))
                break
            else:
                self.data.write_result(row, 'Filed')
                if errorNum < self.Config_Try_Num:
                    errorNum += 1
                    errorInfo = "测试失败,重试中,当前重试次数为第%s次" % (errorNum)
                    self.log.info(self.mylog.out_varname(errorInfo))
                else:
                    self.log.info("重试次数已用完,测试失败")
                    fail_count.append(row)
                    self.log.info(self.mylog.out_varname(fail_count))
                    break

    # 确认最终的断言类型
    def Determine_assert_type(self, response):
        expect_flag = None
        if isinstance(response, str):
            expect_flag = 'str'
        else:
            expect_flag = 'dict'
        return expect_flag

    # 发送邮件、生成测试报告
    def create_test_report(self):
        self.op_testReport.write_TestReport(pass_count,
                                            fail_count)  # 生成excel表格测试报告
        self.op_testReport.excle_to_html()  # 将测试报告转换为html输出
        self.send_mail.send_main(pass_count, fail_count)  # 发送测试报告邮件
示例#2
0
class DependentData(GetData):
    def __init__(self, case_id):
        super().__init__()
        # 实例依赖字段yaml文件
        self.yamlField = OperationYaml('dependFieldInfo.yaml')
        # 数据依赖字段实例
        self.dependFieldYaml = ''
        self.case_id = case_id
        self.data = GetData()
        self.depend_key_dict = {}  #存放依赖key数据

    #执行依赖测试,并返回结果
    def run_dependent(self):
        row_num = self.opera_excle.get_row_num(self.case_id)
        depend_response = self.data.request_info(row_num)
        self.log.info(self.mylog.out_varname(depend_response))
        return depend_response

    #根据依赖key去获取执行依赖测试case的响应,然后返回
    def get_data_for_key(self, row):
        self.depend_data_dict = {}  #执行前清空dict
        yamlDepentKey = self.data.get_depent_key(row)
        # print(yamlDepentKey)
        depend_Field_dict = {}  #数据依赖字段
        # response_data为依赖测试的执行结果
        response_data = self.run_dependent()
        # print(response_data)
        try:
            # ''' 判断depend_data使用eval是否发生异常,如果异常当做单字符处理,
            # 如果没异常判断是否是list且是否为空,满足条件循环处理,否则不处理'''
            if isinstance(response_data, str):
                response_data = eval(response_data)
            if isinstance(yamlDepentKey, list) and yamlDepentKey:
                for i in yamlDepentKey:
                    # print(i)
                    # print(self.depend_key_dict)
                    self.depend_data_parse(i, response_data)
            else:
                return None
        except SyntaxError as syntaxerror:
            print(syntaxerror)
            self.log.error(self.mylog.out_varname(syntaxerror))
            self.depend_data_parse(yamlDepentKey, response_data)
        excleDepentKey = self.dependFiel_kw(row)
        depend_Field_dict[excleDepentKey] = self.depend_key_dict
        # print(self.depend_key_dict)
        return depend_Field_dict

    def depend_data_parse(self, depend_key, response_data):
        '''处理依赖'''
        if depend_key:
            # 定义要获取的key
            # 处理依赖时,只取响应中的第一个值
            __dict = self.data.requestDataDispose.depend_data_parse(
                depend_key, response_data, index='one')
            # 合并字典
            # 确保字典不能为空与类型必须时字典
            if __dict and isinstance(__dict, dict):
                self.depend_key_dict.update(__dict)

    # 返回数据依赖字段
    def dependFiel_kw(self, row):
        col = int(data_config.get_field_depend())
        depend_field = self.opera_excle.get_cell_value(row, col)
        self.dependFieldYaml = '{}{}'.format(self.yaml['dependField'], row)
        # print('self.data.writelist',self.data.writelist)
        if not depend_field:
            # 判断field关键字是否存在存实例中
            if self.dependFieldYaml in self.data.writelist:
                return self.dependFieldYaml
            else:
                return False
        else:
            return depend_field

    # 获取数据依赖字段
    def get_depend_field(self, row):
        self.write_dependField(row)
        depend_field = self.dependFiel_kw(row)
        depend_field_data = self.yamlField.readforKey_onetier(key=depend_field)
        # print(depend_field)
        # print(depend_field_data)
        if depend_field_data:
            return depend_field_data
        else:
            return False

    # 写入数据依赖字段信息至yaml
    def write_dependField(self, row):
        try:
            dependFieldYaml = self.get_data_for_key(row)
            if dependFieldYaml:
                # '''这句代码用于处理yaml写入失败(浮点对象异常的问题)
                value = eval(demjson.encode(dependFieldYaml))
                # print(value)
                self.yamlField.write_yaml(value)
                return True
            else:
                return False
        except BaseException as error:
            print(error)
            self.log.error(self.mylog.out_varname(error))
            return False

    # 将依赖处理完毕的请求数据写入excle
    def writeDependRequestDataToExcle(self, row):
        source_data = self.get_depend_field(row)
        # print(self.mylog.out_varname(source_data))
        falg = None
        if source_data:
            falg = self.data.writeDependFiledToRequestData(
                row, source_data=source_data)
            if falg:
                return True
            else:
                return False
        else:
            return False