python report_python3 BeautifulReport测试报告 及 报告中增加日志输出

原生的HTMLTestRunner很容易找到,偶尔又发现一个更炫酷一点的HTMLTestRunner_PY3,具体详见Github

https://github.com/huilansame/HTMLTestRunner_PY3

BeautifulReport

https://github.com/TesterlifeRaymond/BeautifulReport

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

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

classReportTestResult(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存在内存中的输出,用换行符隔开

defcomplete_output(self):"""Disconnect output redirection and return buffer.

Safe to call multiple times."""

ifself.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() + '\n' + 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值