コード例 #1
0
class RunTest:
    def __init__(self):
        self.run_method = RunMethod()
        self.data = GetData()
        self.com_util = CommonUtil()
        self.send_mai = SendEmail()

    #程序执行的
    def go_on_run(self):
        res = None
        pass_count = []
        fail_count = []
        #10  0,1,2,3
        rows_count = self.data.get_case_lines()
        for i in range(1, rows_count):
            is_run = self.data.get_is_run(i)
            if is_run:
                url = self.data.get_request_url(i)
                method = self.data.get_request_method(i)
                request_data = self.data.get_data_for_json(i)
                expect = self.data.get_expcet_data_for_mysql(i)
                header = self.data.is_header(i)
                depend_case = self.data.is_depend(i)
                if depend_case != None:
                    self.depend_data = DependdentData(depend_case)
                    #获取的依赖响应数据
                    depend_response_data = self.depend_data.get_data_for_key(i)
                    #获取依赖的key
                    depend_key = self.data.get_depend_field(i)
                    request_data[depend_key] = depend_response_data
                if header == 'write':
                    res = self.run_method.run_main(method, url, request_data)
                    op_header = OperationHeader(res)
                    op_header.write_cookie()

                elif header == 'yes':
                    op_json = OperetionJson('../dataconfig/cookie.json')
                    cookie = op_json.get_data('apsid')
                    cookies = {'apsid': cookie}
                    res = self.run_method.run_main(method, url, request_data,
                                                   cookies)
                else:
                    res = self.run_method.run_main(method, url, request_data)
                    # print(res)

                if self.com_util.is_equal_dict(expect, res) == 0:
                    self.data.write_result(i, 'pass')
                    pass_count.append(i)
                else:
                    self.data.write_result(i, res)
                    fail_count.append(i)
        self.send_mai.send_main(pass_count, fail_count)
コード例 #2
0
ファイル: testLogin.py プロジェクト: singlewhip/interfaceTest
class login():
    def __init__(self):
        self.Opexl = OperationExcle('../dataCase/login.xls', 0)
        self.data = GetData()
        self.com_util = CommonUtil()
        self.run_method = RunMethod()

    def test_Login(self):
        rownums = self.data.get_case_line()
        for i in range(1, rownums):
            is_run = self.data.get_is_run(i)
            # print(is_run)
            url = self.data.get_url(i)
            # print(url)
            method = self.data.get_request_method(i)
            request_data = self.data.request_data_type_change(i)
            # print(request_data)
            expect = self.data.get_expect_data(i)
            # print(expect)
            header = self.data.is_header(i)
            select_str = 'select'
            insert_str = 'INSERT'
            update_str = 'UPDATE'
            delete_str = 'DELETE'
            for except_num in select_str, insert_str, update_str, delete_str:
                except_str = self.com_util.is_contain(except_num, expect)
                if except_str is True:
                    expect = self.data.get_sql_expect_data(i)
                else:
                    expect = self.data.get_expect_data(i)
            # print(expect)

            if is_run is True:
                res = self.run_method.run_main(method, url, request_data,
                                               header)
                if except_str is False:
                    if self.com_util.is_contain(expect, res) == True:
                        self.data.write_result(i, 'pass')
                        print("测试通过")
                    else:
                        self.data.write_result(i, 'Filed')
                        print('测试失败')
                # print(res)

                if except_str is True:
                    if self.com_util.is_equal_dict(expect,
                                                   res) == True:  # 判断字典是否相等
                        self.data.write_result(i, 'pass')
                        print('测试通过')
                    else:
                        self.data.write_result(i, res)
                        print('测试失败')
                print(res)
            else:
                pass
コード例 #3
0
ファイル: run_test.py プロジェクト: singlewhip/interfaceTest
class RunTest:
    def __init__(self):
        self.run_method = RunMethod()
        self.data = GetData()
        self.com_util = CommonUtil()
        self.send_mail = SendEmail()
        self.op_testReport = Write_testReport_excle()

    # 程序执行
    def go_on_run(self):
        global pass_count, fail_count
        res = None
        pass_count = []
        fail_count = []
        rows_count = self.data.get_case_line()
        # print(rows_count)
        for i in range(1, rows_count):
            is_run = self.data.get_is_run(i)
            # print(is_run)
            url = self.data.get_url(i)
            # print(url)
            method = self.data.get_request_method(i)
            request_data = self.data.request_data_type_change(i)
            # print(request_data)
            expect = self.data.get_expect_data(i)
            # print(expect)
            header = self.data.is_header(i)
            select_str = 'select'
            insert_str = 'INSERT'
            update_str = 'UPDATE'
            delete_str = 'DELETE'
            for except_num in select_str, insert_str, update_str, delete_str:
                except_str = self.com_util.is_contain(except_num, expect)
                if except_str is True:
                    expect = self.data.get_sql_expect_data(i)
                else:
                    expect = self.data.get_expect_data(i)
            # print(expect)
            errorNum = 0
            if is_run is True:
                while errorNum <= Config_Try_Num:  # 判段失败次数是否小于等于配置失败重试次数
                    res = self.run_method.run_main(method, url, request_data,
                                                   header)
                    if except_str is False:
                        if self.com_util.is_contain(expect, res) == True:
                            self.data.write_result(i, 'pass')
                            print("测试通过")
                            pass_count.append(i)
                            errorNum = 0
                            break
                        else:
                            self.data.write_result(i, 'Filed')
                            # print('测试失败,重试中')

                            if errorNum < Config_Try_Num:
                                errorNum += 1
                                print("测试失败,重试中,当前重试次数为第%s次" % (errorNum))
                            else:
                                print("重试次数已用完,测试失败")
                                fail_count.append(i)
                                break
                    # print(res)

                    if except_str is True:
                        if self.com_util.is_equal_dict(
                                expect, res) == True:  # 判断字典是否相等
                            self.data.write_result(i, 'pass')
                            pass_count.append(i)
                            print('测试通过')
                        else:
                            self.data.write_result(i, 'Filed')
                            # print('测试失败,重试中')
                            if errorNum < Config_Try_Num:
                                errorNum += 1
                                print("测试失败,重试中,当前重试次数为第%s次" % (errorNum))
                            else:
                                print("重试次数已用完,测试失败")
                                fail_count.append(i)
                                break

                else:
                    pass
                print(res)
                # print(self.com_util.is_contain(expect,res)==True)

    # 发送邮件、生成测试报告
    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)  # 发送测试报告邮件
コード例 #4
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)  # 发送测试报告邮件