• python3 BeautifulReport测试报告 及 报告中增加日志输出


    https://blog.csdn.net/chenmozhe22/article/details/82888060#2python_19

    原生的HTMLTestRunner很容易找到,偶尔又发现一个更炫酷一点的HTMLTestRunner_PY3,具体详见Github
    https://github.com/huilansame/HTMLTestRunner_PY3 
    BeautifulReport
    https://github.com/TesterlifeRaymond/BeautifulReport

    BeautifulReport 报告展示中增加日志输出:

    1. 在项目初始化时加入logger,设置日志容器名称要与自己设置的一致

    class ReportTestResult(unittest.TestResult):
        """ override"""
    
        def __init__(self, suite, stream=sys.stdout):
            """ pass """
            super(ReportTestResult, self).__init__()
            self.begin_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
            self.start_time = 0
            self.stream = stream
            self.end_time = 0
            self.failure_count = 0
            self.error_count = 0
            self.loggor = logging.getLogger('kkb')

    2. 在stertTest函数中初始化logging Handler,记录到内存中

        def startTest(self, test) -> None:
            """
                当测试用例测试即将运行时调用
            :return:
            """
            unittest.TestResult.startTest(self, test)
            stdout_redirector.fp = self.outputBuffer
            stderr_redirector.fp = self.outputBuffer
            self.sys_stdout = sys.stdout
            self.sys_stderr = sys.stderr
            # self.sys_stdout = sys.stderr
            sys.stdout = stdout_redirector
            sys.stderr = stderr_redirector
            self.start_time = time.time()
            # ----add logging output-----fancc
            self.log_cap = StringIO()
            self.ch = logging.StreamHandler(self.log_cap)
            self.ch.setLevel(logging.DEBUG)
            myfmt = logging.Formatter(
                '%(asctime)s - %(name)s - "%(filename)s: %(lineno)d" - %(funcName)s - %(levelname)s - %(message)s')
            self.ch.setFormatter(myfmt)
            self.loggor.addHandler(self.ch)

    3. 在complete_output函数的返回值中加入logging存在内存中的输出,用换行符隔开

        def complete_output(self):
            """
            Disconnect output redirection and return buffer.
            Safe to call multiple times.
            """
            if self.sys_stdout:
                sys.stdout = self.sys_stdout
                sys.stderr = self.sys_stdout
                self.sys_stdout = None
                self.sys_stdout = None
            # add log out put ---------fancc
            return self.outputBuffer.getvalue() + '
    ' + self.log_cap.getvalue()

    4. 每个用例执行完后,最好清除handler,在stopTest函数中加入

        def stopTest(self, test) -> None:
            """
                当测试用力执行完成后进行调用
            :return:
            """
            self.end_time = '{0:.3} s'.format((time.time() - self.start_time))
            self.result_list.append(self.get_all_result_info_tuple(test))
            # 清除log的handle----fancc
            self.complete_output()
            self.loggor.removeHandler(self.ch)

    使用方法后,每个用例都有单独的logging记录,不会重复

    记得在前面引入logging模块           import logging

    相关链接: https://www.cnblogs.com/fengf233/p/10871055.html 

  • 相关阅读:
    利用matlab给图像加高斯噪声
    频谱分析代码片段2
    相关性分析代码片段2
    相关性分析代码片段
    频谱分析代码片段
    大脑提取每一个体素26领域的matlab代码
    当前所看论文记录
    论文阅读笔记
    余弦距离、欧氏距离和杰卡德相似性度量的对比分析 by ChaoSimple
    Container With Most Water
  • 原文地址:https://www.cnblogs.com/fcc-123/p/11382429.html
Copyright © 2020-2023  润新知