Python unittest生成測試報(bào)告過程解析
1、先導(dǎo)入HTMLTestRunner模塊
見生成HTMLTestRunner模塊
2、實(shí)例如下
(1)單用例文件執(zhí)行且生成報(bào)告
import unittestimport HTMLTestRunnerclass Study01(unittest.TestCase): def test01(self): print 'test01' def test02(self): self.assertEqual(1,2,msg='1 != 2') def test03(self): print 'test03' def test04(self): print 'test04'if __name__ == ’__main__’: testcases = [Study01('test01'),Study01('test02'),Study01('test03'),Study01('test04')] suit = unittest.TestSuite() suit.addTests(testcases) #測試報(bào)告生成 dir = 'D:test.html' #定義測試報(bào)告文件 filename = open(dir,'wb') #'wb'新建或者打開一個(gè)二進(jìn)制文件,寫入執(zhí)行完的數(shù)據(jù) runner = HTMLTestRunner.HTMLTestRunner(stream=filename, , description=u'測試用例明細(xì)') #調(diào)用HTMLTestRunner類定義測試報(bào)告內(nèi)容 runner.run(suit) #調(diào)用HTMLTestRunner類下面的run()方法運(yùn)行用例套件 filename.close() #關(guān)閉測試報(bào)告文件
(2)批量執(zhí)行用例且生成測試報(bào)告
import unittestimport HTMLTestRunnerdef all_case(): case_dir = 'D:work_docpycharm2python_Basics' #用例存放路徑 discover=unittest.defaultTestLoader.discover(case_dir, pattern='XFS*.py', top_level_dir=None) return discoverif __name__ == '__main__': dir = 'd:test1.html' filename = open(dir,'wb') runner = HTMLTestRunner.HTMLTestRunner(stream=filename, , description='description') runner.run(all_case())
3、解釋
wb:只寫打開或新建一個(gè)二進(jìn)制文件;只允許寫數(shù)據(jù)。 stream:測試報(bào)告寫入文件的存儲(chǔ)路徑 title:測試報(bào)告的主題 description:測試報(bào)告的描述以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. HTML5 Canvas繪制圖形從入門到精通2. HTTP協(xié)議常用的請求頭和響應(yīng)頭響應(yīng)詳解說明(學(xué)習(xí))3. 不要在HTML中濫用div4. XML入門的常見問題(三)5. HTML DOM setInterval和clearInterval方法案例詳解6. HTML5實(shí)戰(zhàn)與剖析之觸摸事件(touchstart、touchmove和touchend)7. CSS清除浮動(dòng)方法匯總8. 本站用的rss輸出9. Vue如何使用ElementUI對表單元素進(jìn)行自定義校驗(yàn)及踩坑10. XML在語音合成中的應(yīng)用
