用python寫自動化測試時,unittest框架與BeautifulReport結合可以生成完美的可視化測試報告html
【第一步】:準備好BeautifulReport,git地址:python
https://github.com/TesterlifeRaymond/BeautifulReportgit
若是本身電腦上安裝了git,能夠直接使用git命令克隆到本地指定目錄下github
git clone https://github.com/TesterlifeRaymond/BeautifulReport
克隆到python安裝目錄的/Lib/site-packages/目錄下;web
若是沒有安裝git,直接在git上下載BeautifulReport.ZIP 到本地python的的/Lib/site-packages/目錄下框架
【第二步】:組織用例並套用BeautifulReport測試
這裏用到unittest.defaultTestLoader.discover()方法批量處理整合測試套件,再用BeautifulReport()方法執行用例。代碼以下:code
import unittest from BeautifulReport import BeautifulReport if __name__ == '__main__':
case_path = os.path.join(os.getcwd(), "testcases") discover = unittest.defaultTestLoader.discover(case_path, pattern="inter*.py", top_level_dir=None) # 測試報告寫入路徑 report_dir = "F:\\work\\linkcld\\lds\\report\\" result = BeautifulReport(discover) result.report(filename='test_report', description='整合後的接口自動化測試報告', log_path=report_dir)
解釋:htm
report ( filename -> 測試報告名稱, 若是不指定默認文件名爲report.html ;description -> 測試報告用例名稱展現 ;log_path='.' -> 測試報告文件寫入路徑 )blog
完成!
web自動化測試使用此測試報告的時候,用例執行失敗還可以進行截圖,後續試過再記錄。