Python unittest生成測試報告過程解析
1、先導入HTMLTestRunner模塊
見生成HTMLTestRunner模塊
2、實例如下
(1)單用例文件執行且生成報告
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) #測試報告生成 dir = 'D:test.html' #定義測試報告文件 filename = open(dir,'wb') #'wb'新建或者打開一個二進制文件,寫入執行完的數據 runner = HTMLTestRunner.HTMLTestRunner(stream=filename, , description=u'測試用例明細') #調用HTMLTestRunner類定義測試報告內容 runner.run(suit) #調用HTMLTestRunner類下面的run()方法運行用例套件 filename.close() #關閉測試報告文件
(2)批量執行用例且生成測試報告
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:只寫打開或新建一個二進制文件;只允許寫數據。 stream:測試報告寫入文件的存儲路徑 title:測試報告的主題 description:測試報告的描述以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。
相關文章:
1. Python獲取抖音關注列表封號賬號的實現代碼2. ajax請求添加自定義header參數代碼3. Python數據分析之pandas函數詳解4. 解決Python 進程池Pool中一些坑5. php測試程序運行速度和頁面執行速度的代碼6. 無線標記語言(WML)基礎之WMLScript 基礎第1/2頁7. 三個不常見的 HTML5 實用新特性簡介8. 使用.net core 自帶DI框架實現延遲加載功能9. php網絡安全中命令執行漏洞的產生及本質探究10. Warning: require(): open_basedir restriction in effect,目錄配置open_basedir報錯問題分析
