Exemplo n.º 1
0
def case_01_OTI_001(sys_addr, intf_url_dict):
    data_type = 'xml'
    file_name = 'aaa.txt'
    headers = eval(ReadYaml(comm_file).get_value('headers'))
    report_no = get_report_no()
    # 请求体数据从文件中读取,可放入data_path下层目录
    req_body = ReadFile(file_name, data_path).get_file_value()
    # 读取接口代码
    req_code = ReqResHandle(
        data_type, req_body,
        'RequestType').get_fields_value().get('RequestType')
    # 接口地址
    intf_url = ReadYaml().get_value(sys_addr) + intf_url_dict.get(req_code)

    scene_name, step, respond, assert_result, remark = None, None, None, None, None
    try:
        # 请求初始化
        req_body = IntfInitEngine().start_init_engine(data_type, req_code,
                                                      req_body)

        # 执行请求
        respond = RequestBase('post', intf_url, req_body,
                              headers).get_respond_body()

        # 获取当前函数的注释
        scene_name = case_01_OTI_001.__doc__.strip()
        step = 'step_01:xxx'
        remark = file_name

        # 断言,先从响应中获取对应字段的值,再进行比较
        assert_result = 'xxx'

    except:
        assert False
        pass
    finally:
        # 保存结果至数据库
        value_list = [
            report_no, scene_name, step, intf_url, req_body, respond,
            assert_result, remark
        ]
        add_lc_business_result(value_list)
Exemplo n.º 2
0
def html_to_pdf(report_no, system_name):
    """
    将 html 页面转换为 pdf,注意:需要本地安装wkhtmltopdf.exe,并设置路径才能转换pdf
    :param report_no: 报告号
    :param system_name: 系统名称
    """
    path_wk = ReadYaml().get_value('wkhtmltopdf_path')
    html_file_path = get_report_no_file(report_no, report_path)
    pdf_file_path = get_file_full_path(report_pdf_path, 'report.pdf',
                                       report_no, system_name)
    config = pdfkit.configuration(wkhtmltopdf=path_wk)
    with open(html_file_path, 'r', encoding='utf-8') as f:
        pdfkit.from_file(f, pdf_file_path, configuration=config)
        f.close()
Exemplo n.º 3
0
    def __init__(self):
        """
        数据库基类
        """
        self.host = ReadYaml().get_value('database.host')
        self.port = ReadYaml().get_value('database.port')
        self.user = ReadYaml().get_value('database.user')
        self.password = ReadYaml().get_value('database.password')
        self.db_name = ReadYaml().get_value('database.name')
        self.charset = ReadYaml().get_value('database.charset')

        self.conn = pymysql.connect(host=self.host, port=self.port, user=self.user, password=self.password,
                                    database=self.db_name, charset=self.charset)
        self.cursor = self.conn.cursor()
Exemplo n.º 4
0
def multi_execute(sys_addr, file_name):
    # 相关参数初始化
    # 设置线程数(在init文件中设置)
    executor = ThreadPoolExecutor(max_workers=20)
    # 接口编号与接口地址对应关系字典,===内容已删除===
    url_dict = {}
    report_no = TimeStyle().cur_time
    print(report_no)
    file_path = os.path.join(data_path, file_name)

    # excel读取报文
    book = xlrd.open_workbook(file_path)
    tp_table = book.sheet_by_index(0)

    nrows = tp_table.nrows

    print('==============开始读取数据,共计[%s]条==============' % nrows)
    req_msg_list = []
    for i in range(nrows):
        req_msg = ''
        if i < 1:
            continue
        req_msg = tp_table.cell(i, 3).value
        # 读取接口代码
        req_code = ReqResHandle(
            'xml', req_msg,
            'RequestType').get_fields_value().get('RequestType')
        # 接口地址

        intf_url = ReadYaml().get_value(sys_addr) + url_dict.get(req_code)
        # 请求初始化
        req_msg = IntfInitEngine().start_init_engine('xml', req_code, req_msg)

        req_msg_list.append((report_no, str(i), intf_url, req_msg))

    print('==============读取数据完成,开始多线程执行==================')
    for n in range(len(req_msg_list)):
        executor.submit(func_handle, req_msg_list[n])
Exemplo n.º 5
0
    :param discover: 所有测试用例
    :param report_no: 测试执行批次号(时间戳)
    :param system_name: 系统名称
    :param title: 测试报告标题
    """
    html_name = get_file_full_path(report_path, 'result.html', report_no,
                                   system_name)
    fp = open(html_name, 'wb')
    runner = HTMLTestReportCN.HTMLTestRunner(stream=fp,
                                             title=title,
                                             tester='wyun')
    runner.run(discover)
    fp.close()


@threads(ReadYaml().get_value('max_workers'))
def multi_run_test_suite(discover, report_no, system_name, case_name):
    """
    多线程执行用例
    :param discover: 所有测试用例
    :param report_no: 测试执行批次号(时间戳)
    :param system_name: 系统名称
    :param case_name: 用例名称
    """
    cur_date_path = get_host_date_path(report_path)
    file_name = 'result_' + system_name + '_' + report_no + '.html'
    result = BeautifulReport(discover)
    result.report(filename=file_name,
                  description=case_name,
                  log_path=cur_date_path)
Exemplo n.º 6
0
    def start_run(self, save_file):
        """
        开始执行测试用例
        :param save_file: 测试用例执行结果保存文件名称
        :return:
        """
        # 1. 第一步读取excel数据,读取模板
        excel_data = ExcelHandle().read_excel_data(self.excel_path,
                                                   self.sheet_name)
        template_list = ExcelHandle().copy_excel_template(
            self.excel_path, self.sheet_name)
        template_book = template_list[0]
        template = template_list[1]

        nrows = len(excel_data)
        # 2. 获取所有场景及对应的步骤数,并存到字典中; 使用列表按顺序存放场景名称,执行时按此顺序
        for n in range(nrows):
            # 读取 业务场景编号
            scene_code = excel_data[n][1]
            # 场景编号不能为空
            if scene_code == '':
                break
            # 添加到 列表中
            if scene_code not in self.scene_index:
                self.scene_index.append(scene_code)
            # 将场景及对应的步骤数添加到字典中
            if scene_code not in self.scene_all.keys():
                self.scene_all[scene_code] = 1
            else:
                self.scene_all[scene_code] += 1
        # 3. 场景循环处理
        # 行 计数器,用于记录当前执行的行索引值
        row = 0
        # 第一重循环,按场景
        for scene in self.scene_index:
            # 提取响应参数存储的字典,场景开始前初始化
            response_after = dict()
            # 从字典中获取当前场景步骤数
            step_num = self.scene_all[scene]
            # 第二重循环,按步骤
            for step in range(1, step_num + 1):
                # 行索引计数器开始计数
                row += 1
                # 4. 读取导入excel的各个字段数据
                # 是否执行
                is_execute = excel_data[row - 1][14]
                # 判断用例是否执行
                if is_execute.lower() != 'yes':
                    continue

                # 拼接接口请求url
                req_url = ReadYaml().get_value(
                    self.system_addr) + excel_data[row - 1][7]
                # 5. 开始执行
                try:
                    # 请求初始化
                    req_body = IntfInitEngine().start_init_engine(
                        excel_data[row - 1][8], excel_data[row - 1][4],
                        excel_data[row - 1][10], **response_after)

                    # 执行请求
                    res = RequestBase(
                        excel_data[row - 1][6], req_url, req_body,
                        eval(excel_data[row - 1][9])).get_respond()
                    res_head = res.headers
                    if res.content:
                        res_body = res.text
                    else:
                        res_body = ''

                    # 断言
                    if res_body == '':
                        assert_result, remark = 'False', '无响应信息'
                    else:
                        assert_result, remark = intf_base_assert(
                            excel_data[row - 1][8], res_body,
                            eval(excel_data[row - 1][15]))

                    # 后置条件 提取参数
                    if excel_data[row - 1][12]:
                        response_after = ReqResHandle(
                            excel_data[row - 1][8], res_body,
                            excel_data[row - 1][12],
                            response_after).get_fields_value()
                    # 写入excel中
                    template.write(row, 15, req_body, style('style2'))
                    template.write(row, 16, str(res_head), style('style2'))
                    template.write(row, 17, res_body, style('style2'))
                    template.write(row, 18, str(response_after),
                                   style('style2'))
                    template.write(row, 19, assert_result, style('style2'))
                    template.write(row, 20, remark, style('style2'))
                except:
                    # 写入excel中
                    template.write(row, 20, '未知异常,执行失败', style('style2'))
        # 保存文件
        file_name = get_file_full_path(details_path,
                                       save_file,
                                       name2=self.system_addr)
        template_book.save(file_name)
Exemplo n.º 7
0
            fh_stream.setFormatter(formatter)
            # 写入文件
            fh_file = logging.FileHandler(self.log_file)
            fh_file.setLevel(log_level_mapper[self.log_level])
            fh_file.setFormatter(formatter)

            # 添加handler
            self.logger.addHandler(fh_stream)
            self.logger.addHandler(fh_file)


# 当前时间
current_time = TimeStyle().cur_date

# 扩展名
log_extension = ReadYaml().get_value('log.extension')
logfile = os.path.join(log_path, current_time + log_extension)

# 日志文件级别
level = ReadYaml().get_value('log.level')


# 对外调用该方法,写入日志
def my_log(log_name=__file__):
    return Logger(log_file=logfile, log_name=log_name, log_level=level).logger


if __name__ == "__main__":
    my_log().debug("this is a debug")
    my_log().info('this is a info.')
    my_log().error('error....')