Python unittest生成測(cè)試報(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) #測(cè)試報(bào)告生成 dir = 'D:test.html' #定義測(cè)試報(bào)告文件 filename = open(dir,'wb') #'wb'新建或者打開一個(gè)二進(jìn)制文件,寫入執(zhí)行完的數(shù)據(jù) runner = HTMLTestRunner.HTMLTestRunner(stream=filename, , description=u'測(cè)試用例明細(xì)') #調(diào)用HTMLTestRunner類定義測(cè)試報(bào)告內(nèi)容 runner.run(suit) #調(diào)用HTMLTestRunner類下面的run()方法運(yùn)行用例套件 filename.close() #關(guān)閉測(cè)試報(bào)告文件
(2)批量執(zhí)行用例且生成測(cè)試報(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:測(cè)試報(bào)告寫入文件的存儲(chǔ)路徑 title:測(cè)試報(bào)告的主題 description:測(cè)試報(bào)告的描述以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. ASP動(dòng)態(tài)網(wǎng)頁制作技術(shù)經(jīng)驗(yàn)分享2. jsp文件下載功能實(shí)現(xiàn)代碼3. asp.net core項(xiàng)目授權(quán)流程詳解4. 在JSP中使用formatNumber控制要顯示的小數(shù)位數(shù)方法5. CSS3實(shí)現(xiàn)動(dòng)態(tài)翻牌效果 仿百度貼吧3D翻牌一次動(dòng)畫特效6. XMLHTTP資料7. ASP常用日期格式化函數(shù) FormatDate()8. html中的form不提交(排除)某些input 原創(chuàng)9. CSS3中Transition屬性詳解以及示例分享10. ASP基礎(chǔ)入門第八篇(ASP內(nèi)建對(duì)象Application和Session)
