unittest+python3.x使用HTMLTestRunner輸出html測試報告
阿新 • • 發佈:2019-01-24
這裡使用的是python3.x,則需要相應的HTMLTestRunner.py檔案,否則不能輸出html測試報告
HTMLTestRunner.py檔案下載連線如下
將下載的檔案放入python安裝目錄下的Lib目錄即可
示例如下:
先設定應用的函式檔案mathfunc.py
def add(a,b):
return a+b
然後編寫測試指令碼檔案test_mathfunc.py
import unittest from mathfunc import * import HTMLTestRunner class TestMathFunc(unittest.TestCase): def test_01(self): self.assertEqual(3,add(1,4)) def test_02(self): self.assertEqual(5,add(1,4)) if __name__ == '__main__': filepath = '../report/htmlreport.html' ftp=open(filepath,'wb') suite = unittest.TestSuite() suite.addTest(TestMathFunc('test_01')) suite.addTest(TestMathFunc('test_02')) runner=HTMLTestRunner.HTMLTestRunner(stream=ftp,title='welcome to this web') runner.run(suite) unittest.main()
filepath的引數是指定輸出測試報告html檔案的位置。
ftp表示開啟檔案路徑,‘wb’是引數,不可省略。