1. 程式人生 > 實用技巧 >allure 測試報告(三)基於 Python pytest

allure 測試報告(三)基於 Python pytest

一、安裝

pip install allure-pytest

該命令將安裝 allure-pytest 和 allure-python-commons 程式包,以生成與 Allure 2 相容的報告資料。

二、使用

要使 Allure 在測試執行過程中收集結果,只需新增 --alluredir 選項並提供路徑即可儲存結果。

# 收集測試結果
pytest --alluredir=/tmp/my_allure_results

# 生成allure臨時報告並開啟預設瀏覽器顯示生成的報告
allure serve /tmp/my_allure_results

三、allure 支援 pytest 的功能

allure 支援 pytest 常見的一些功能,報告xfails、fixtures、finalizers、marks,、conditional skips、parametrization。

3.1 xfails 標記為預期失敗

# 標記失敗用例為預期失敗
@pytest.mark.xfail(condition=lambda: True, reason='this test is expecting failure')
def test_xfail_expected_failure():
    assert False

# 標記成功用例為預期失敗
@pytest.mark.xfail(condition=lambda
: True, reason='this test is expecting failure') def test_xfail_unexpected_pass(): assert True

當預期測試失敗時,會導致測試被跳過,並在 Tags 中進行特殊標記

對於測試通過的用例也會在 Tags 中進行特殊標記

3.2 fixture 和finalizer

3.3 引數化 parametrize

使用 parametrize 標記的測試用例的所有引數名稱和值都將在報告中捕獲

@pytest.mark.parametrize('param1', [True, False])
@pytest.mark.parametrize(
'param2', ['value1', 'value2']) def test_parametrize_with_two_parameters(param1, param2): pass