Example #1
0
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
def write_excle():
    Gd = GetData()
    row=Gd.get_case_line()
    for i in range(2,row+1):
    # for i in range(16,row+1):
        caseid = Gd.get_caseId(i)
        if caseid:
            falg=write_dependField(i)
            # print(falg)
            if not falg:
                write_dependSql(i)
Example #3
0
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)  # 发送测试报告邮件
class Write_testReport_excle():
    global workbook, worksheet, chart, formatter, title_formatter, ave_formatter, now, filename
    now = time.strftime("%Y-%m-%d %H-%M-%S")
    workbook_path = "../report/excle_report/"
    if not os.path.exists(workbook_path):
        os.mkdir(workbook_path)
    workbook = xlsxwriter.Workbook(workbook_path + now + '_test_report.xls')
    filename = '../report/excle_report/' + now + '_test_report.xls'
    worksheet = workbook.add_worksheet("测试报告")
    # 创建一个图表对象,column:柱形图
    chart = workbook.add_chart({'type': 'column'})

    # # 定义平均值栏数据格式对象:边框加粗1像素,数字按2位小数显示
    # ave_formatter = workbook.add_format()
    # ave_formatter.set_border(1)
    # ave_formatter.set_num_format('0.00')
    def __init__(self):
        self.Ope = OperationExcle()
        self.data = GetData()
        # self.workbook=workbook
        # self.worksheet=worksheet
    def create_TestReport(self):
        worksheet.set_column("A:ZZ", 20)
        # bold = workbook.add_format({"bold": True})
        # 定义标题栏格式对象:边框加粗1像素,背景色为灰色,单元格内容居中、加粗,自动换行
        formatter = workbook.add_format()
        formatter.set_border(1)
        formatter.set_text_wrap()
        title_formatter = workbook.add_format()
        title_formatter.set_border(1)
        title_formatter.set_bg_color('#cccccc')
        title_formatter.set_align('center')
        title_formatter.set_bold()
        title_formatter.set_text_wrap()
        title = ['系统名称', '通过接口个数', '失败接口个数', '全部接口个数', '测试通过率', '测试失败率']
        buname = ['SCM3.0', '居家小二app', '发货宝', '运营后台系统', '直营oms系统']
        worksheet.write_row("A1", title, title_formatter)
        worksheet.write_column("A2", buname, formatter)

    def write_TestReport(self, pass_list, fail_list):
        pass_num = float(len(pass_list))
        fail_num = float(len(fail_list))
        all_num = pass_num + fail_num
        pass_result = "%.2f%%" % (pass_num / all_num * 100)
        fail_result = "%.2f%%" % (fail_num / all_num * 100)
        data = [[pass_num, fail_num, all_num, pass_result, fail_result],
                [pass_num, fail_num, all_num, pass_result, fail_result],
                [pass_num, fail_num, all_num, pass_result, fail_result],
                [pass_num, fail_num, all_num, pass_result, fail_result],
                [pass_num, fail_num, all_num, pass_result, fail_result]]
        #添加柱形图
        list1 = ('B', 'C', 'D')
        for row_num in list1:
            chart.add_series({
                "name":
                "=测试报告!${}$1".format(row_num, row_num),
                "categories":
                "=测试报告!$A$2:$A$6",
                "values":
                "=测试报告!${}$2:${}$6".format(row_num, row_num)
            })
        # 添加柱状图标题
        chart.set_title({"name": "各个系统接口测试报告"})
        # Y轴名称
        chart.set_y_axis({"name": "接口数量明细"})
        # X轴名称
        chart.set_x_axis({"name": "系统名称"})
        # 图表样式
        chart.set_style(10)
        #设置图表大小
        chart.set_size({'width': 600, 'height': 400})
        # 插入图表带偏移
        worksheet.insert_chart('G2', chart, {'x_offset': 25, 'y_offset': 10})
        # 定义标题栏格式对象:边框加粗1像素,背景色为灰色,单元格内容居中、加粗,自动换行
        formatter = workbook.add_format()
        formatter.set_border(1)
        formatter.set_text_wrap()
        # 写入第2到第6行的数据,并将第2~6行数据加入图表系列
        for i in range(2, 7):
            worksheet.write_row('B{}'.format(i), data[i - 2], formatter)
        self.create_TestReport()
        self.write_faild_to_excle()
        # workbook.close()
    def write_faild_to_excle(self):
        rows_count = self.data.get_case_line()
        #定义标题栏格式对象:边框加粗1像素,背景色为灰色,单元格内容居中、加粗,自动换行
        formatter = workbook.add_format()
        formatter.set_border(1)
        formatter.set_font_color('red')
        formatter.set_text_wrap()
        for i in range(1, rows_count):
            #将失败的用例写入测试报告中
            if not self.data.get_result(i) == 'pass':
                # print(self.Ope.get_row_values(i))
                # print(self.data.get_result(i))
                worksheet.write_row('A{}'.format(i + 8),
                                    self.Ope.get_row_values(i), formatter)
            else:
                pass
                # worksheet.write_row('A{}'.format(i+8+rows_count), self.Ope.get_row_values(i),formatter)
        workbook.close()

    def excle_to_html(self):
        # 注意这里不能直接使用workbook,因为直接引用workbook返回的对象不是一个文件路径,而是:<class 'xlsxwriter.workbook.Workbook'>
        fp = pandas.ExcelFile(filename)
        df = fp.parse()
        html_report_path = '../report/html_report/'
        if not os.path.exists(html_report_path):
            os.mkdir(html_report_path)
        with codecs.open(html_report_path + now + '_test_report.html', 'w',
                         'utf-8') as html_file:
            html_file.write(df.to_html(header=True, index=False))
Example #5
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)  # 发送测试报告邮件