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. WMLScript的語法基礎(chǔ)2. html小技巧之td,div標(biāo)簽里內(nèi)容不換行3. msxml3.dll 錯(cuò)誤 800c0019 系統(tǒng)錯(cuò)誤:-2146697191解決方法4. ASP中if語句、select 、while循環(huán)的使用方法5. XML入門的常見問題(四)6. php bugs代碼審計(jì)基礎(chǔ)詳解7. ASP使用MySQL數(shù)據(jù)庫的方法8. xml中的空格之完全解說9. ASP中解決“對象關(guān)閉時(shí),不允許操作。”的詭異問題……10. ASP動(dòng)態(tài)網(wǎng)頁制作技術(shù)經(jīng)驗(yàn)分享
