Unitest框架的使用(四)HTMLTestRunner輸出測試報告

輸出測試報告

當我們測試完了,肯定要輸出測試報告,HTMLTestRunner是Unittest框架下的一個擴展,它用來生成HTML測試報告

  1. 下載HTMLTestRunner.py文件,地址:http://tungwaiyip.info/software/HTMLTestRunner.html
  2. 將下載的文件放在Python安裝目錄下的Lib文件夾中,我的是:D:\Python\Python37\Lib 這個目錄
下載地址:http://tungwaiyip.info/software/HTMLTestRunner.html
(選中該py文件,單擊鼠標右鍵,另存爲本地)
  1. 修改HTMLTestRunner.py文件,因爲這個庫是Python2編寫的
第94行		將import StringIO		修改成import io
第539行	將self.outputBuffer = StringIO.StringIO()		修改成self.outputBuffer= io.StringIO()
第631行	將print >> sys.stderr, ‘\nTime Elapsed: %s‘ %(self.stopTime-self.startTime)		
				修改成print(sys.stderr, ‘\nTimeElapsed: %s‘ % (self.stopTime-self.startTime))
第642行	將if not rmap.has_key(cls):修改成if not cls in rmap:
第766行	將uo = o.decode(‘latin-1‘)修改成uo = e
第772行	將ue=e.decode('latin-1')修改爲ue=e

  1. 導出模塊import HTMLTestRunner
from HTMLTestRunner import HTMLTestRunner
  1. 測試報告輸出路徑
    time = time.strftime("%Y%m%d%H%M%S")
    path = os.path.dirname(os.path.realpath(__file__))
    filename = path + '\\' + time + 'Test_Result.html'
  1. 在測試代碼尾部編寫HTMLTestRunner方法
	fp = open(filename, "wb")
    runner = HTMLTestRunner(stream=fp, title=u"MathTest測試報告", description=u"用例執行情況")
    runner.run(suite)
    fp.close()

我們用上次的工程來進行修改,輸出測試報告,代碼如下

import os
import time
import unittest
from HTMLTestRunner import HTMLTestRunner
from Test_Case import MathCase
from Test_Case2 import MathCase2

if __name__ == "__main__":
    suite = unittest.TestSuite()
    "裝載測試用例"
    suite.addTest(MathCase("test_add"))
    suite.addTest(MathCase("test_sub"))
    suite.addTest(MathCase2("test_mul"))
    suite.addTest(MathCase2("test_div"))

    """
    報告時間爲當前時間,並給出輸出格式
    測試路徑爲當前工程路徑
    文件命名爲路徑+\\+當前時間+test_result.html
    """
    time = time.strftime("%Y%m%d%H%M%S")
    path = os.path.dirname(os.path.realpath(__file__))
    filename = path + '\\' + time + 'Test_Result.html'
    fp = open(filename, "wb")
    runner = HTMLTestRunner(stream=fp, title=u"MathTest測試報告", description=u"用例執行情況")
    runner.run(suite)
    fp.close()

運行結果
在這裏插入圖片描述
工程目錄打開剛輸出的測試報告
在這裏插入圖片描述
summary,Failed,All,這三個可以展開查看詳細的情況
在這裏插入圖片描述

參考:https://blog.csdn.net/wxy_summer/article/details/52814791

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章