unittest(生成測試報告)

2022-09-07 07:06:13 字數 1799 閱讀 6505

1、先導入htmltestrunner模組

2、例項如下

(1)單用例檔案執行且生成報告

import

unittest

import

htmltestrunner

class

study01(unittest.testcase):

deftest01(self):

print

"test01

"def

test02(self):

self.assertequal(1,2,msg="

1 != 2")

deftest03(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,

title="

testcase report",

description=u"

測試用例明細

") #

呼叫htmltestrunner類定義測試報告內容

runner.run(suit) #

呼叫htmltestrunner類下面的run()方法執行用例套件

filename.close() #

關閉測試報告檔案

(2)批量執行用例且生成測試報告

import

unittest

import

htmltestrunner

defall_case():

case_dir = "

d:\work_doc\pycharm2\python_basics"#

用例存放路徑

discover=unittest.defaulttestloader.discover(case_dir,

pattern="

xfs*.py",

top_level_dir=none)

return

discover

if__name__ == "

__main__":

dir = "

d:\\test1.html

"filename = open(dir,"wb"

) runner = htmltestrunner.htmltestrunner(stream=filename,

title="

test report",

description="

description")

runner.run(all_case())

3、解釋

unittest 管理用例生成測試報告

登入方法的封裝 from time import sleep from python selenium.slide import swipeleft def login user,pwd caps dr webdriver.remote caps dr.implicitly wait 10 slee...

unittest詳解(七) 自動生成測試報告

用例執行完成後,執行結果預設是輸出在螢幕上,其實我們可以把結果輸出到乙個檔案中,形成測試報告。unittest自帶的測試報告是文字形式的,如下 import unittest if name main 測試用例目錄 test dir r d git test framework test case ...

unittest框架及整合測試報告

import time import unittest from base.box import email,htmlemailattachment from base.html test runner import htmltestrunner class testrunner def runne...