HTMLTestRunner加入logging输出
使用HTMLTestRunner生成html的測試報告的時候,報告中只有console輸出,logging的輸出無法保存,
如果要在報告中加入每一個測試用例執(zhí)行的logging信息,則需要改HTMLTestRunner的源碼
?
HTMLTestRunner原作者文件下載地址:http://tungwaiyip.info/software/HTMLTestRunner.html
這里使用findyou的美化版來做實驗,github地址https://github.com/findyou/HTMLTestRunnerCN/tree/dev
?
在HTMLTestReportCN.py 474行加入一個logger,可以自己傳入一個logger,這里固定一個
class _TestResult(TestResult):# note: _TestResult is a pure representation of results.# It lacks the output and reporting ability compares to unittest._TextTestResult.def __init__(self, verbosity=1):TestResult.__init__(self)self.stdout0 = Noneself.stderr0 = Noneself.success_count = 0self.failure_count = 0self.error_count = 0self.verbosity = verbosity# result is a list of result in 4 tuple# (# result code (0: success; 1: fail; 2: error),# TestCase object,# Test output (byte string),# stack trace,# )self.result = []#增加一個測試通過率 --Findyouself.passrate=float(0)self.logger = logging.getLogger('mylog')在488行startTest函數(shù)中初始化logging.Handler,記錄到內(nèi)存中
def startTest(self, test):TestResult.startTest(self, test)# just one buffer for both stdout and stderrself.outputBuffer = io.StringIO()stdout_redirector.fp = self.outputBufferstderr_redirector.fp = self.outputBufferself.stdout0 = sys.stdoutself.stderr0 = sys.stderrsys.stdout = stdout_redirectorsys.stderr = stderr_redirector#----add logging output----fengf233self.log_cap = io.StringIO()self.ch = logging.StreamHandler(self.log_cap)self.ch.setLevel(logging.DEBUG)formatter = logging.Formatter('[%(levelname)s][%(asctime)s] [%(filename)s]->[%(funcName)s] line:%(lineno)d ---> %(message)s')self.ch.setFormatter(formatter)self.logger.addHandler(self.ch)在496行?complete_output函數(shù)的返回值中加入logging存在內(nèi)存中的輸出,用換行符隔開
def complete_output(self):"""Disconnect output redirection and return buffer.Safe to call multiple times."""if self.stdout0:sys.stdout = self.stdout0sys.stderr = self.stderr0self.stdout0 = Noneself.stderr0 = None#add log out put ---fengf233return self.outputBuffer.getvalue()+'\n'+self.log_cap.getvalue()每個用例執(zhí)行完后,最好清除handler,在504行stopTest函數(shù)中加入
def stopTest(self, test):# Usually one of addSuccess, addError or addFailure would have been called.# But there are some path in unittest that would bypass this.# We must disconnect stdout in stopTest(), which is guaranteed to be called.a = self.complete_output()#清除log的handle---fengf233 self.logger.removeHandler(self.ch)return a使用這個方法也不用去改html的代碼,集成在每個用例的a中返回,效果如下
每個用例都是單獨logging記錄,不會重復(fù)
HTMLTestReportCN.py 中輸出是居中,覺得不好看,可以在414行中更改標簽,增加style="text-align:left"屬性
<div id='div_%(tid)s' class="collapse in" style="text-align:left"><pre>%(script)s</pre></div>別忘了在最前面import logging
最后只需要在你需要logging輸出的文件位置加上logging就可以了,但是需要注意,這里我是使用mylog名稱的logger,你創(chuàng)建的logger需要同名
所以這里HTMLTestRunner還有增加傳入logger的提升空間,這里不做增加了
logger = logging.getLogger(logger=‘mylog’)?
轉(zhuǎn)載于:https://www.cnblogs.com/fengf233/p/10871055.html
總結(jié)
以上是生活随笔為你收集整理的HTMLTestRunner加入logging输出的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 第六次实训作业
- 下一篇: 洛谷P1095守望者的逃离题解-伪动态规