使用 HTMLTestRunner 模塊可以生成測試報告,但是系統自帶的報告不詳細,不好看,所以找了一份詳細的報告 HTMLTestRunNer 模板,直接導入就能使用
兩種方法生成HTML報告,都是一個意思,用那種都一樣
import unittest from datetime import datetime # 獲取時間模塊 from test.HTMLTestRunnerNew import HTMLTestRunner # 導入報告模板 # 創建自動識別套件,加載測試用例 one_suite = unittest.defaultTestLoader.discover(".") # .代表獲取當前py文件所在的路徑的測試用例,也可以使用絕對路徑 # 指定HTML報告生成的路徑及文件名+報告名中顯示具體時間 report_full_path = "./reports/" + "report_" + datetime.strftime(datetime.now(), "%Y-%m-%d %H-%M-%S") + ".html" # # 方法一 # # 打開的文件對象傳給save_to_file # save_to_file = open(report_full_path, mode='wb') # # 讀寫文件 # one_runner = HTMLTestRunner(stream=save_to_file, # title="某項目自動化測試", # verbosity=2, # description="對系統流程進行回歸測試", # tester="守護往昔") # one_runner.run(one_suite) # 使用加載器run方法來運行套件 # save_to_file.close() # 關閉文件 # 方式二:推薦使用 # 使用with 上下文管理,open打開文件,mode='wb':以二進制寫入 as 接受返回的對象 with open(report_full_path, mode='wb') as save_to_file: # HTMLTestRunner:相當於創建HTML運行器a # stream默認輸入到控制台,指定輸出到文件對象stream=save_to_file # title=報告的標題 # verbosity=是為了指定報告的詳細程度, 0, 1, 2=最詳細 # description=報告的描述信息 # tester = 測試人員的名字 # runner 創建執行器對象 one_runner = HTMLTestRunner(stream=save_to_file, title="某項目自動化測試", verbosity=2, description="對系統流程進行回歸測試", tester="守護往昔") one_runner.run(one_suite)
報告名稱會顯示帶有具體時間的報告:report_2020-01-07 12-29-47.html
使用瀏覽器打開報告顯示樣式:
百度網盤獲取報告模板:
鏈接:https://pan.baidu.com/s/1m-vInhEfSzTaTnUsUCEVJg
提取碼:088p
2、txt 報告
3、BeautifulReport模塊:https://pypi.org/project/BeautifulReport/