1. 程式人生 > >selenium使用HTMLTestRunner生成測試報告

selenium使用HTMLTestRunner生成測試報告

Coding .py ESS image col 技術分享 .html evel res

HTMLTestRunner下載地址:

  • python2:http://tungwaiyip.info/software/HTMLTestRunner.html
  • python3:https://pan.baidu.com/s/1dEZQ0pz

將下載好的HTMLTestRunner.py文件放到python安裝目錄的Lib文件夾中

#coding=utf-8
import unittest
import HTMLTestRunner
import time

def creatsuite():
    testunit = unittest.TestSuite()
    test_address = 
C:\\Users\\user\\Desktop\\python_study\\script #定義discover方法的參數 discover = unittest.defaultTestLoader.discover( test_address, pattern=un_*.py, top_level_dir=None ) for test_suite in discover: for test_case in test_suite: testunit.addTests(test_case)
return testunit if __name__ == __main__: testtime = time.strftime(%Y%m%d_%H%M%S_) #獲取當前時間 #報告的保存地址及報告名稱 repo_add=C:/Users/user/Desktop/python_study/report/+testtime+result.html fp = file(repo_add,wb) runner = HTMLTestRunner.HTMLTestRunner( stream=fp, title
=Test Report, description=Test Result) runner.run(creatsuite()) fp.close() #關閉報告文件

如果生成的測試報告名字相同,後生成的測試報告會覆蓋前面的測試報告,為了保證報告不重名,在報告名字中加入時間戳

testtime = time.strftime(%Y%m%d_%H%M%S_)

repo_add=C:/Users/user/Desktop/python_study/report/+testtime+result.html

輸出的測試報告:

技術分享圖片

技術分享圖片

selenium使用HTMLTestRunner生成測試報告