引言
在很早之前,我写过一篇文章关于HTMLTestRunner报告优化的处理:https://www.cnblogs.com/liudinglong/p/12346824.html,后面群友在群里问了一个问题,是关于通过和失败的用例,图标样式和单击响应的问题。虽然很早之前已经帮群友解决了,由于之前工作繁忙,然而一直没有写下来,现在写下来,以便帮助更多的小伙伴们少走弯路。
问题
下面就是群友的问题:
群友说我的博客没有关于这个问题的处理方法,好吧,先来看看是什么问题:
针对群友的截图,发现两个问题:
问题一:饼图位置不太友好
问题二:单击通过没有反应,或单击通过后,也没有显示日志。
解决方案
首先进入报告模板源码中,找到报告_generate_report_test,具体如下:
def _generate_report_test(self, rows, cid, tid, n, t, o, e):
# e.g. 'pt1.1', 'ft1.1', etc
has_output = bool(o or e)
# ID修改点为下划线,支持Bootstrap折叠展开特效 - Findyou
tid = (n == 0 and 'p' or 'f') + 't%s_%s' % (cid+1,tid+1)
name = t.id().split('.')[-1]
if self.verbosity > 1:
doc = t.shortDescription() or ""
else:
doc = ""
desc = doc and ('%s: %s' % (name, doc)) or name
if not PY3K:
if isinstance(desc, str):
desc = desc.decode("utf-8")
# tmpl = has_output and self.REPORT_TEST_WITH_OUTPUT_TMPL or self.REPORT_TEST_NO_OUTPUT_TMPL
tmpl = has_output and (n==0 and self.REPORT_TEST_NO_OUTPUT_TMPL or self.REPORT_TEST_WITH_OUTPUT_TMPL) or self.REPORT_TEST_NO_OUTPUT_TMPL
# utf-8 支持中文 - Findyou
# o and e should be byte string because they are collected from stdout and stderr?
if isinstance(o, str):
# uo = unicode(o.encode('string_escape'))
if PY3K:
uo = o
else:
uo = o.decode('utf-8', 'ignore')
else:
uo = o
if isinstance(e, str):
# ue = unicode(e.encode('string_escape'))
if PY3K:
ue = e
elif e.find("Error") != -1 or e.find("Exception") != -1:
es = e.decode('utf-8', 'ignore').split('\n')
es[-2] = es[-2].decode('unicode_escape')
ue = u"\n".join(es)
else:
ue = e.decode('utf-8', 'ignore')
else:
ue = e
script = self.REPORT_TEST_OUTPUT_TMPL % dict(
id=tid,
output=saxutils.escape(uo + ue),
)
if getattr(t, 'imgs', []):
# 判断截图列表,如果有则追加
tmp = u""
for i, img in enumerate(t.imgs):
if i == 0:
tmp += """ <img src="https://img-blog.csdnimg.cn/2022010703045056739.jpg" style="display: block;" class="img"/>\n""" % img
else:
tmp += """ <img src="https://img-blog.csdnimg.cn/2022010703045056739.jpg" style="display: none;" class="img"/>\n""" % img
imgs = self.IMG_TMPL % dict(imgs=tmp)
else:
imgs = u"""无截图"""
row = tmpl % dict(
tid=tid,
Class=(n == 0 and 'hiddenRow' or 'none'),
style=n == 2 and 'errorCase' or (n == 1 and 'failCase' or 'passCase'),
desc=desc,
script=script,
status=self.STATUS[n],
img=imgs,
)
rows.append(row)
if not has_output:
return
def _generate_ending(self):
return self.ENDING_TMPL
这个函数主要处理测试结果如果展示在报告上的,整个看不懂没关系,只需要加个代码:
n == 2是通过的用例,就输出REPORT_TEST_WITH_OUTPUT_TMPL,否则输出REPORT_TEST_NO_OUTPUT_TMPL,
然后将REPORT_TEST_NO_OUTPUT_TMPL的内容改成与REPORT_TEST_WITH_OUTPUT_TMPL一直,再将button渲染成:btn-success。
接下来就是饼图了,修改位置:
优化后的报告
优化后,我们再运行一下脚本,查看报告:
效果图:
回复群友的截图:
自此,HTML报告优化到此结束。另外,需要报告文件的或喜欢自动化,测试开发的小伙伴可以加入我们学习交流QQ群:696400122。