一、pytest allure視頻
從視頻中我們可以清晰地了解pytest+allure的基本使用方法:
# 安裝pytest pip install pytest # 安裝pytest的allure插件 pip install pytest-allure2 # 執行pytest測試並生成allure報告 pytest --alluredir=./report allure serve ./report
通過上面的步驟,我們就能快速上手pytest+allure的使用了。
二、pytest allure自動化框架
pytest+allure不僅是一個測試工具,還是一個自動化測試框架。它支持模塊化測試、並發測試等功能,可以輕鬆應對大規模的測試任務。
舉個例子,我們可以使用pytest的fixture機制來管理測試用例中的前置條件和後置條件:
fixture使用裝飾器@pytest.fixture定義。 fixture也可以用autouse參數,這樣就不需要在測試用例中顯示調用。 @pytest.fixture() def test_env(): return 'test' @pytest.fixture() def prod_env(): return 'prod' def test_login(test_env): print(f"login with {test_env}") def test_logout(test_env): print(f"logout with {test_env}")
在上面的代碼中,我們定義了兩個fixture:test_env和prod_env,這兩個fixture都返回了字符串類型的環境變量。在test_login和test_logout函數中,我們使用了test_env來打印日誌。
三、pytest allure截圖
pytest+allure還支持截圖功能,我們可以在測試用例執行失敗時,自動截圖並將截圖添加到allure報告中:
@pytest.mark.parametrize('username,password', [('admin', '123456'), ('user', '654321')]) def test_login(username, password): if username != 'admin' or password != '123456': allure.attach(driver.get_screenshot_as_png(), name="test_failed", attachment_type=allure.attachment_type.PNG) assert False, 'login failed'
在上面的代碼中,我們使用了pytest的parametrize機制來執行多次測試。如果測試用例執行失敗,我們就使用allure.attach來截圖,並在assert語句中拋出異常。
四、pytest allure報告
pytest+allure提供了漂亮的HTML報告,我們可以查看測試用例執行結果、用例執行時間、用例失敗原因等信息。同時支持在報告中自行添加環境信息、測試人員信息等。
以下是一個示例報告:
請見附件
五、pytest allure自定義
pytest+allure還支持自定義操作,我們可以根據自己的需求添加自定義的步驟、注釋、標籤等信息:
from allure_commons.types import LabelType def test_case_add_label(): # step allure.step("step 1") assert True # tag allure.dynamic.label('test-case-id', 'TC_UI_001', LabelType.SEVERITY) # attachment allure.attach('HTML attachment', 'attach with HTML type', allure.attachment_type.HTML)
在上面的代碼中,我們使用了allure.step添加了一個自定義步驟,使用了allure.dynamic.label為測試用例添加了一個標籤,使用了allure.attach添加了一個HTML形式的附件。
六、pytest allure生成報告
執行以下命令,即可生成allure報告:
allure generate report_files -o report allure open report
其中,report_files是allure報告文件的路徑,-o表示輸出路徑。
七、pytest allure報告對比
pytest+allure還支持報告對比功能,我們可以將不同版本測試結果進行對比,以便更好地評估產品質量。
以下是一個示例對比報告:
請見附件
綜上所述,pytest+allure是一個功能強大的自動化測試框架,它提供了豐富的測試機制、自定義操作、測試報告等功能。如果你還沒有使用過pytest+allure,建議嘗試一下,相信其會成為你自動化測試的得力助手。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/301409.html