Python Allure庫的使用示例教程
Python Allure庫是一個開源的跨平臺的測試報告框架,用于生成漂亮、易于閱讀和易于理解的測試報告。它支持Python的標(biāo)準(zhǔn)TestRunner框架并且可以生成不同的報告格式,如HTML報告、json報告等。本文將圍繞著Python Allure庫來進(jìn)行講解,讓大家了解它的用途和主要功能。
一、安裝Python Allure庫
要使用Python Allure庫,需要在命令行中安裝Allure命令行工具。
brew install allure
安裝完Allure命令行工具后,可通過pip安裝Python Allure庫。
pip install allure-pytest
二、使用Python Allure庫
1.簡單示例
import pytest import allure @allure.step("參數(shù)相加 : {0},{1}") def add(x, y): return x + y @pytest.mark.parametrize('x', [0, 1]) @pytest.mark.parametrize('y', [2, 3]) def test_add(x, y): with allure.step("步驟1:輸入兩個參數(shù)"): print("輸入?yún)?shù):x->{},y->{}".format(x, y)) with allure.step("步驟2:調(diào)用相加方法"): result = add(x, y) with allure.step("步驟3:輸出結(jié)果"): print("輸出結(jié)果:{}".format(result)) if __name__ == '__main__': pytest.main(['-s', '-q', '--alluredir', './report/']) import pytest import allure @allure.step("參數(shù)相加 : {0},{1}") def add(x, y): return x + y @pytest.mark.parametrize('x', [0, 1]) @pytest.mark.parametrize('y', [2, 3]) def test_add(x, y): with allure.step("步驟1:輸入兩個參數(shù)"): print("輸入?yún)?shù):x->{},y->{}".format(x, y)) with allure.step("步驟2:調(diào)用相加方法"): result = add(x, y) with allure.step("步驟3:輸出結(jié)果"): print("輸出結(jié)果:{}".format(result)) if __name__ == '__main__': pytest.main(['-s', '-q', '--alluredir', './report/'])
在腳本中首先引入pytest和allure庫,然后使用@allure.step包裝每個測試步驟,使用@allure.parametrize注釋來測試方法的步驟,最后使用pytest.main運行腳本,運行結(jié)果將生成在“./report/”文件夾中。
2.定制化報告
Python Allure庫提供了多種注釋,來增加測試報告的可讀性,讓報告更加直觀。
使用@allure.feature注釋來制定測試特性:
@allure.feature("加法運算測試") def test_add(): pass
使用@allure.story注釋來制定測試場景:
@allure.story("測試加法") def test_add(): pass
3.報告展示
在生成報告后,可以通過運行命令在瀏覽器中查看報告。
allure serve 報告目錄
三、總結(jié)
Python Allure庫是一個實用可靠的測試報告框架,它幾乎可以與Python的其他庫和框架無縫集成。利用Python Allure庫,可以輕松生成易于閱讀的測試報告,讓測試變得更加簡單便捷。
到此這篇關(guān)于Python Allure庫的使用的文章就介紹到這了,更多相關(guān)Python Allure庫使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python爬取網(wǎng)站數(shù)據(jù)保存使用的方法
這篇文章主要介紹了使用Python從網(wǎng)上爬取特定屬性數(shù)據(jù)保存的方法,其中解決了編碼問題和如何使用正則匹配數(shù)據(jù)的方法,詳情看下文2013-11-11在Python的Django框架中simple-todo工具的簡單使用
這篇文章主要介紹了在Python的Django框架中simple-todo工具的簡單使用,該工具基于原web.py中的開源項目,需要的朋友可以參考下2015-05-05python 實現(xiàn)讀取一個excel多個sheet表并合并的方法
今天小編就為大家分享一篇python 實現(xiàn)讀取一個excel多個sheet表并合并的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-02-02