使用unittest框架的腳本執行完成后,會生成一個html格式的報告
這個報告是提前制作了一個html的模板,然后將對應的內容寫入到模板中,並生成一個最終的報告,這個報告模板在通過 pip install BeautifulReport后,就會在下面路徑中存在:
C:\Program Files\Python37\Lib\site-packages\BeautifulReport\template,這個html模板可以將里面的一些表格屬性名稱修改為適合自己的名稱,例如:
1 <body class="gray-bg"> 2 <div class="row border-bottom white-bg dashboard-header"> 3 <div class="col-sm-12 text-center"> 4 <span style="${title}">自動化測試報告</span> 5 </div> 6 </div> 7 <div class="wrapper wrapper-content animated fadeInRight"> 8 <div class="row"> 9 <div class="col-sm-12"> 10 <div class="ibox float-e-margins"> 11 <div class="ibox-title"> 12 <h5 style="${sub-title}">報告匯總</h5> 13 <div class="ibox-tools"> 14 <a class="collapse-link"> 15 <i class="fa fa-chevron-up"></i> 16 </a> 17 <a class="close-link"> 18 <i class="fa fa-times"></i> 19 </a> 20 </div> 21 </div> 22 <div class="ibox-content"> 23 <div class="row"> 24 <div class="col-sm-6 b-r" style="height:350px"> 25 <form class="form-horizontal"> 26 <div class="form-group"> 27 <label class="col-sm-2 control-label text-info">測試目的:</label> 28 <div class="col-sm-5"> 29 <span class="form-control" id="testName"></span> 30 </div> 31 </div> 32 <div class="form-group"> 33 <label class="col-sm-2 control-label text-info">用例總數:</label> 34 <div class="col-sm-5"> 35 <span class="form-control" id="testAll"></span> 36 </div> 37 </div> 38 <div class="form-group"> 39 <label class="col-sm-2 control-label text-navy">用例通過:</label> 40 <div class="col-sm-5"> 41 <span class="form-control" id="testPass"></span> 42 </div> 43 </div> 44 <div class="form-group"> 45 <label class="col-sm-2 control-label text-danger">用例失敗:</label> 46 <div class="col-sm-5"> 47 <span class="form-control text-danger" id="testFail"></span> 48 </div> 49 </div> 50 <div class="form-group"> 51 <label class="col-sm-2 control-label text-warning">用例跳過:</label> 52 <div class="col-sm-5"> 53 <span class="form-control text-warning" id="testSkip"></span> 54 </div> 55 </div> 56 <div class="form-group"> 57 <label class="col-sm-2 control-label text-info">開始時間:</label> 58 <div class="col-sm-5"> 59 <span class="form-control" id="beginTime"></span> 60 </div> 61 </div> 62 <div class="form-group"> 63 <label class="col-sm-2 control-label text-info">運行時間:</label> 64 <div class="col-sm-5"> 65 <span class="form-control" id="totalTime"></span> 66 </div> 67 </div> 68 </form> 69 </div> 70 <div class="col-sm-6"> 71 <div style="height:350px" id="echarts-map-chart"></div> 72 </div> 73 </div> 74 </div> 75 </div> 76 </div> 77 </div>
那么通過這個報告生成,都可以做哪些呢,比較指定存儲路徑,報告主題等,下面直接看BeautifulReport代碼,發現要對這個類實例化時,必須要先傳入一個suite(也就是測試用例集),然后調用這個類的report方法進行報告生成時,可以傳入哪些參數:description, filename: str = None, report_dir='.', log_path=None, theme='theme_default',除去 log_path廢棄后,可以有4個參數進行傳入,每個參數的具體用法在代碼中都有詳細說明,這里不再重復。
1 class BeautifulReport(ReportTestResult, PATH): 2 img_path = 'img/' if platform.system() != 'Windows' else 'img\\' 3 4 def __init__(self, suites): 5 super(BeautifulReport, self).__init__(suites) 6 self.suites = suites 7 self.report_dir = None 8 self.title = '自動化測試報告' 9 self.filename = 'report.html' 10 11 def report(self, description, filename: str = None, report_dir='.', log_path=None, theme='theme_default'): 12 """ 13 生成測試報告,並放在當前運行路徑下 14 :param report_dir: 生成report的文件存儲路徑 15 :param filename: 生成文件的filename 16 :param description: 生成文件的注釋 17 :param theme: 報告主題名 theme_default theme_cyan theme_candy theme_memories 18 :return: 19 """ 20 if log_path: 21 import warnings 22 message = ('"log_path" is deprecated, please replace with "report_dir"\n' 23 "e.g. result.report(filename='測試報告_demo', description='測試報告', report_dir='report')") 24 warnings.warn(message) 25 26 if filename: 27 self.filename = filename if filename.endswith('.html') else filename + '.html' 28 29 if description: 30 self.title = description 31 32 self.report_dir = os.path.abspath(report_dir) 33 os.makedirs(self.report_dir, exist_ok=True) 34 self.suites.run(result=self) 35 self.stopTestRun(self.title) 36 self.output_report(theme) 37 text = '\n測試已全部完成, 可打開 {} 查看報告'.format(os.path.join(self.report_dir, self.filename)) 38 print(text)
下面列舉調用這個模塊的實現方法:
1 # -*- coding:utf-8 -*- 2 ''' 3 # @Time : 2019/12/3 16:50 4 # @Author : nihaoya 5 # @FileName: WeiBo_test.py 6 # @Software: PyCharm 7 ''' 8 import os 9 import time 10 import unittest 11 from BeautifulReport import BeautifulReport as bf 12 13 class WeiBo(unittest.TestCase): 14 此處省略 15 16 if __name__ == "__main__": 17 suite = unittest.TestLoader().loadTestsFromTestCase(WeiBo) 18 run = bf(suite) 19 run.report(filename=u"微博測試報告_" + time.strftime("%Y~%m~%d %H~%M~%S"), description=u"以游客形式瀏覽微博", report_dir="report", theme="theme_memories")