引言
在很早之前,我寫過一篇文章關於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="data:image/jpg;base64,%s" style="display: block;" class="img"/>\n""" % img else: tmp += """ <img src="data:image/jpg;base64,%s" 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。