無論做什麼自動化,
測試
報告一定要有的,它可以清楚地展示出來我們執行用例的情況。便於檢視
自動化測試
結果內容。安靜這邊瞭解目前透過
python
生成的測試報告分別有:HTMLTestRunner、BeautifulReport 、pytest-html和Allure,這幾種報告內容都是屬於不同的模板,本篇
文章
主要介紹下這如何生成以上四份報告的過程以及對比情況。
HTMLTestRunner
HTMLTestRunner是Python標準庫的unittest模組的擴充套件。它生成易於使用的HTML測試報告。使用時需要下載,然後放到專案目錄中
下載完成後,我們開啟檔案,其中有一段程式碼是告訴我們如何進行使用。
# output to a file
fp = file(‘my_report。html’, ‘wb’)
runner = HTMLTestRunner。HTMLTestRunner(
stream=fp,
title=‘My unit test’,
description=‘This demonstrates the report output by HTMLTestRunner。’
)
# Use an external stylesheet。
# See the Template_mixin class for more customizable options
runner。STYLESHEET_TMPL = ‘’
# run the test
runner。run(my_test_suite)
從上面程式碼中可以看出來,報告需要三個引數:
·stream:表示生成報告的路徑
·title:表示報告的標題
·description:表示用例執行情況說明
安靜來一段例項程式碼,來生成詳細的測試報告:
import unittest
import HTMLTestRunner_cn
class Test(unittest。TestCase):
def test_01(self):
‘’‘測試用例01’‘’
print(‘——-用例01——-’)
def test_02(self):
print(‘——-用例02——-’)
def test_03(self):
‘’‘測試用例03’‘’
print(‘——-用例03——-’)
if __name__ == ‘__main__’:
# 測試報告地址
fp = open(‘result。html’, “wb”)
# 報告詳情
runner = HTMLTestRunner_cn。HTMLTestRunner(stream=fp,
title=u‘自動化測試報告,測試結果如下:’,
description=u‘用例執行情況:’)
# 例項化
testunit = unittest。TestSuite()
# 載入用例
testunit。addTests(unittest。TestLoader()。loadTestsFromTestCase(Test))
# 執行用例
runner。run(testunit)
# 關閉報告
fp。close()
執行完指令碼後,本地路徑出現了一個測試報告,雙擊開啟可以檢視報告詳情,其中用例中加入註釋,可以方便配置用例詳情顯示。
BeautifulReport
BeautifulReport也是基於unittest中的一個報告框架,也是以HTML的報告形式進行展示出來的,可以透過pip直接進行安裝下載。
安裝:pip install BeautifulReport
驗證安裝:pip show BeautifulReport
開啟BeautifulReport的原始碼可以看到報告中已經將需要的引數列舉下來了。
以上引數可以預設也可以進行自己手動修改。
import unittest
import BeautifulReport
class Test(unittest。TestCase):
def test_01(self):
‘’‘測試用例01’‘’
print(‘——-用例01——-’)
def test_02(self):
print(‘——-用例02——-’)
def test_03(self):
‘’‘測試用例03’‘’
print(‘——-用例03——-’)
if __name__ == ‘__main__’:
testunit = unittest。TestSuite()
# 載入用例
testunit。addTests(unittest。TestLoader()。loadTestsFromTestCase(Test))
result = BeautifulReport。BeautifulReport(testunit)
result。report(filename=‘report’, description=‘測試報告’, log_path=None)
透過執行後可以看出生成了和HTMLTestRunner類似的測試報告。
pytest-html
pytest-html屬於pytest第三方外掛,使用時需要安裝pytest和pytest-html。
安裝pytest:pip install pytest-html
安裝pytest-html:pip installpytest-html
pytest-html的原始碼地址:https://github。com/pytest-dev/pytest-html
使用方法:
需要在執行的用例下透過終端形式輸入命令 pytest ——html=report。html (等號後面表示生成報告的路徑和名稱)。
class Test:
def test_01(self):
‘’‘測試用例01’‘’
print(‘——-用例01——-’)
def test_02(self):
print(‘——-用例02——-’)
def test_03(self):
‘’‘測試用例03’‘’
print(‘——-用例03——-’)
透過執行後,會在當前目錄中生成一個report。html。
雙擊進行開啟報告,可以看出詳情的測試報告內容。可以看到和HTMLtestRunner的報告類似。
Allure
Allure也是屬於一種開源的測試報告框架,基於多種語言,其中在python中是透過pytest單元測試框架進行生成的。也是目前測試行業中常用到的測試報告內容。其中allure是的環境是基於java的環境(下載JDK,配置環境變數)。
allure下載地址:https://github。com/allure-framework/allure2
下載後透過解壓,將allure中的bin檔案放入到環境變數中即可進行使用
我們是透過pytest單元測試框架進行生成allure報告的,需要安裝第三方外掛 allure-pytest,可以透過pip直接進行安裝 pip install allure-pytest。
class Test_Allure:
def test_01(self):
‘’‘測試用例01’‘’
print(‘——-用例01——-’)
def test_02(self):
print(‘——-用例02——-’)
def test_03(self):
‘’‘測試用例03’‘’
print(‘——-用例03——-’)
想要生成測試報告需要先在執行
測試用例
時候,在命令列中加入生成報告的命令。
pytest ——alluredir 報告目錄
執行完後,會在當前目錄中生成一個詳細的report的報告目錄,裡面存放的就是執行用例的測試結果內容。
再次輸入命令:
allure serve 測試結果目錄
透過執行命令後,會在當前的
瀏覽器
中直接開啟我們的測試報告內容。
總結
透過本篇文章,安靜已經將目前市面上常用到的測試框架進行了總結,我想透過大家對圖片中的報告認知,可能會感覺,HTMLtestRunner和BeautifulReport,pytest-html生成的報告內容基本差不多,將測試結果可以在一個頁面中全部展示出來,但是我們的allure報告,存在多個頁面內容,如果測試用例較多的話,可以進行檢視更加方便快捷。好了,具體用到那個報告,就看個人愛好了~感謝大家閱讀,希望本篇文章對您有所幫助!
請關注+私信回覆:“測試”就可以免費拿到軟體測試學習資料,同時進入群學習交流~~