基于HTMLTestRunner0.8.2版本修改HTMLTestRunner.py
一、适配python3.5以上
94行修改import StringIo为import io
539行修改self.outputBuffer = StringIo.StringIO() 为 self.outputBuffer = io.StringIO()
631行修改 print >> sys.stderr, ‘\nTime Elapsed: %s’ % (self.stopTime-self.startTime) 为 print(sys.stderr, ‘\nTime Elapsed: %s’ % (self.stopTime-self.startTime))
642行修改 if not rmap.has_key(cls):为if not cls in rmap:
766行修改 uo = o.decode(‘latin-1’) 修改成 uo = e
775行修改 ue = o.decode(‘latin-1’) 修改成 ue = e
1、暴力解决问题
1.1 结果为pass也进行输出,但只输出print打印的结果
760行修改 tmpl = has_output and self.REPORT_TEST_WITH_OUTPUT_TMPL or self.REPORT_TEST_NO_OUTPUT_TMPL 为
if n == 2:
tmpl = has_output and self.REPORT_TEST_WITH_OUTPUT_TMPL
else:
tmpl = self.REPORT_TEST_WITH_OUTPUT_TMPL
766行修改 uo = e 修改成 uo = o
1.2 添加测试人员(添加其他变量均可以如此)
610行添加一个变量tester
672行添加(‘Tester’, self.tester)
2、优雅解决问题
"""
对HTMLTestRunner中HTMLTestRunner方法进行继承和重载
"""
from xml.sax import saxutils
import sys
from HtmlTestRunner import HTMLTestRunner
class HTMLTestRunnerEdit(HTMLTestRunner):
def __init__(self, stream=sys.stdout, verbosity=1, title=None, description=None, tester=None):
"""
增加变量测试人员
"""
super().__init__(stream=stream, verbosity=verbosity, title=title, description=description)
if tester is not None:
self.tester = tester
else:
self.tester = '测试'
def getReportAttributes(self, result):
"""
Return report attributes as a list of (name, value).
Override this to add custom attributes.
"""
startTime = str(self.startTime)[:19]
duration = str(self.stopTime - self.startTime)
status = []
if result.success_count:
status.append('Pass %s' % result.success_count)
if result.failure_count:
status.append('Failure %s' % result.failure_count)
if result.error_count:
status.append('Error %s' % result.error_count)
if status:
status = ' '.join(status)
else:
status = 'none'
return [
('Start Time', startTime),
('Duration', duration),
('Status', status),
('Tester', self.tester),
]
def _generate_report_test(self, rows, cid, tid, n, t, o, e):
"""
重载后解决:
1、pass状态也能输出结果
"""
# e.g. 'pt1.1', 'ft1.1', etc
has_output = bool(o or e)
tid = (n == 0 and 'p' or 'f') + 't%s.%s' % (cid+1, tid+1)
name = t.id().split('.')[-1]
doc = t.shortDescription() or ""
desc = doc and ('%s: %s' % (name, doc)) or name
if n == 2:
tmpl = has_output and self.REPORT_TEST_WITH_OUTPUT_TMPL
else:
tmpl = self.REPORT_TEST_WITH_OUTPUT_TMPL
# o and e should be byte string because they are collected from stdout and stderr?
uo = o
ue = e
script = self.REPORT_TEST_OUTPUT_TMPL % dict(
id=tid,
output=saxutils.escape(uo+ue),
)
row = tmpl % dict(
tid=tid,
Class=(n == 0 and 'hiddenRow' or 'none'),
style=n == 2 and 'errorCase' or (n == 1 and 'failCase' or 'none'),
desc=desc,
script=script,
status=self.STATUS[n],
)
rows.append(row)
if not has_output:
return