原生的HTMLTestRunner很容易找到,偶爾又發現一個更炫酷一點的HTMLTestRunner_PY3,具體詳見Github
https://github.com/huilansame/HTMLTestRunner_PY3
BeautifulReport
https://github.com/TesterlifeRaymond/BeautifulReport
本文主要描述如何去使用這兩個報告,以及我之前遇到的一些坑
文章目錄
一.HTMLTestRunner_PY3的使用
(1)文件存放位置
下載完這個文件,放置在你需要你常用的虛擬環境對應的路徑下面、Lib文件夾下面即可
放置完之後,在pycharm裏面導入
from HTMLTestRunner_PY3 import HTMLTestRunner
如果沒有報錯,則如上報告模板導入正確,注意最終import的還是原生的HTMLTestRunner
(2)使用方法一(普通方式運行python導出測試報告)
如果按照如上方式運行,使用unittest框架去運行,是永遠都無法導出測試報告,原因是:
PyCharm會默認使用自帶的unittest框架來執行單元測試,不會執行main函數中的代碼,所以不生成測試報告
具體可參考如下文章:
https://blog.csdn.net/autotest00/article/details/80747123
如上文章是一種方式,直接添加普通的python模式運行,但是更建議的方式是,把運行測試報告單獨的代碼單獨放在一個文件集成運行
最終測試報告如下:
(3)測試報告的代碼與單元測試文件分離(推薦方式)
目錄結果如下:
manner.py文件
from HTMLTestRunner_PY3 import HTMLTestRunner
import unittest
import os
current_path = os.getcwd() # 獲取當前路徑
cash_path = os.path.join(current_path, "TestCase") # 設置用例路徑
report_path = os.path.join(current_path, "Report") # 設置報告存放路徑
# 加載測試用例的方法
def load_all_case():
discover = unittest.defaultTestLoader.discover(cash_path, pattern="*test.py")
return discover
if __name__ == '__main__':
# 報告地址&名稱拼接到一起
report_title = 'Example用例執行報告.html'
result_path = os.path.join(report_path, report_title)
# 報告描述
desc = '用於展示修改樣式後的HTMLTestRunner'
# 使用result_path,既能創建文件,又指定了路徑
with open(result_path, 'wb') as report:
runner = HTMLTestRunner(stream=report, title=report_title, description=desc)
runner.run(load_all_case()) # 最終在run()方法裏面執行load_all_case方法加載測試用例
(4)加載測試用例方式二
from HTMLTestRunner_PY3 import HTMLTestRunner
import unittest
from alien_test import Alien_Test
import os
current_path = os.getcwd()
cash_path = os.path.join(current_path, "TestCase")
report_path = os.path.join(current_path, "Report")
if __name__ == '__main__':
# 報告地址&名稱
report_title = 'Example用例執行報告_2.html'
result_path = os.path.join(report_path, report_title)
# 報告描述
desc = '用於展示修改樣式後的HTMLTestRunner'
testsuite = unittest.TestSuite()
# 把所有的測試用例加載到testsuit裏面,loadTestsFromTestCase()方法裏面填寫類的名稱,則全部測試用例都自動加載
testsuite.addTest(unittest.TestLoader().loadTestsFromTestCase(Alien_Test))
with open(result_path, 'wb') as report:
runner = HTMLTestRunner(stream=report, title=report_title, description=desc)
runner.run(testsuite) # 執行的使用,run()方法加載testsuit即可
(5)加載測試用例方式三
from HTMLTestRunner_PY3 import HTMLTestRunner
import unittest
from alien_test import Alien_Test
import os
current_path = os.getcwd()
cash_path = os.path.join(current_path, "TestCase")
report_path = os.path.join(current_path, "Report")
if __name__ == '__main__':
# 報告地址&名稱
report_title = 'Example用例執行報告_3.html'
result_path = os.path.join(report_path, report_title)
# 報告描述
desc = '用於展示修改樣式後的HTMLTestRunner'
testsuite = unittest.TestSuite()
testsuite.addTest(Alien_Test('test_6_alien')) # 一個一個添加測試用例,且按照添加的順序執行
testsuite.addTest(Alien_Test('test_1_alien'))
testsuite.addTest(Alien_Test('test_2_alien'))
with open(result_path, 'wb') as report:
runner = HTMLTestRunner(stream=report, title=report_title, description=desc)
runner.run(testsuite)
(6)測試報告名稱+時間方法
from HTMLTestRunner_PY3 import HTMLTestRunner
import unittest
from alien_test import Alien_Test
import os
import time
current_path = os.getcwd()
cash_path = os.path.join(current_path, "TestCase")
report_path = os.path.join(current_path, "Report")
if __name__ == '__main__':
now = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()))
# 報告地址&名稱
report_title = 'Example報告' + now + ".html"
result_path = os.path.join(report_path, report_title)
# 報告描述
desc = '用於展示修改樣式後的HTMLTestRunner'
testsuite = unittest.TestSuite()
testsuite.addTest(Alien_Test('test_6_alien'))
testsuite.addTest(Alien_Test('test_1_alien'))
testsuite.addTest(Alien_Test('test_2_alien'))
with open(result_path, 'wb') as report:
runner = HTMLTestRunner(stream=report, title=report_title, description=desc)
runner.run(testsuite)
最終報告的名稱爲:Example報告2018-09-28 17:38:26.html
(7)拓展方法
result = runner.run(testsuite)
num1 = result.testsRun # 運行測試用例的總數
num2 = result.success_count # 運行測試用例成功的個數
num3 = result.failure_count # 運行測試用例失敗的個數
print(num1,num2,num3)
二.BeautifulReport的使用
(1)下載地址及安裝位置
https://github.com/TesterlifeRaymond/BeautifulReport
下載完之後,按照官方文檔說明,把文件存放在python對應的安裝位置下面Lib/site-package目錄下,如下所示
但是,我僅僅把下載的文件存放在這個位置之後,還是不能導出這個包,最後發現還需要把下載文件夾裏面的BeautifulReport.py這個文件單獨再存放在Lib目錄下面,這樣這樣就可以導包了。
如果只把BeautifulReport.py放在*/Lib目錄下,沒有如上下載的文件包,也不行,會報錯如下:
說明,在BeautifulReport.py文件運行的時候,還是需要下載包裏面的template這個模板文件的,最終才能運行。
針對一直無法導入包的問題,查找了一些文章很實用
http://baijiahao.baidu.com/s?id=1596345816517002361&wfr=spider&for=pc
https://www.cnblogs.com/hhudaqiang/p/6596043.html
(2)使用方式
from BeautifulReport import BeautifulReport
import unittest
from alien_test import Alien_Test
import os
import time
current_path = os.getcwd()
report_path = os.path.join(current_path, "Report")
now = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()))
# 報告地址&名稱
report_title = 'Example報告' + now + ".html" # 如果不能打開這個文件,可能是now的格式,不支持:和空格
# 報告描述
desc = '用於展示修改樣式後的HTMLTestRunner'
if __name__ == '__main__':
testsuite = unittest.TestSuite()
testsuite.addTest(unittest.makeSuite(Alien_Test))
run = BeautifulReport(testsuite)
run.report(description=desc, filename=report_title, log_path=report_path)
最終導出的報告如下: