Python生成HTML測試報告
阿新 • • 發佈:2018-11-30
HTMLTestRunner是Python標準庫提供unittest單元測試框架的一個擴充套件,他生成易於使用的HTML測試報告。這個擴充套件只是一HTMLTest Runner.py 檔案
1.下載地址:http://tungwaiyip.info/software/HTMLTestRunner.html
(選中該py檔案,單擊滑鼠右鍵,另存為本地)
2.安裝方法:將下載的檔案儲存到..\python35\Lib目錄下
- 1
- 2
- 3
- 4
修改HTMLTestRunner.py檔案(因為HTMLTestRunner是基於Python2開發的)
3. 修改辦法: 第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
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
程式碼如下:
#unittest測試框架
import unittest
from HTMLTestRunner import HTMLTestRunner
import time
class Test(unittest.TestCase):
#用於測試用例執行前的初始化工作
def setUp(self):
print("test start")
def test_bbb(self):
print("test bbb")
def test_aaa (self):
print("test aaa")
#用於測試用例執行之後的善後工作
def tearDown(self):
print("test end")
if __name__ == '__main__':
suite=unittest.TestSuite()
suite.addTest(Test("test_bbb"))
suite.addTest(Test("test_aaa"))
now=time.strftime("%Y-%m-%d %H_%M_%S")
#定義報告存放路徑
filename='D:\ ' + now +'result.html'
fp=open(filename,'wb')
#定義測試報告
runner=HTMLTestRunner(stream=fp,title='測試報告',description='用例執行情況:')
runner.run(suite)
fp.close()#關閉報告檔案
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
用例執行完成後,開啟result.html檔案檢視生成的測試報告