天天看點

軟體測試丨Allure2報告中添加用例支援tags标簽、失敗重試功能

作者:霍格沃茲軟體測試

本文為霍格沃茲測試開發學社學員學習筆記分享

原文連結:allure2報告中添加用例支援tags标簽、失敗重試功能 L2 - 學習筆記 - 測試人社群

1、Allure2 報告中添加用例支援 tags 标簽

Allure2 添加用例标簽應用場景

  • Allure 報告支援的一些常見 Pytest 特性包括 xfail、skipif、fixture 等。測試結果會展示特定的辨別在用例詳情頁面。
軟體測試丨Allure2報告中添加用例支援tags标簽、失敗重試功能
軟體測試丨Allure2報告中添加用例支援tags标簽、失敗重試功能

Allure2 添加用例标簽-xfail、skipif

  • 用法:使用裝飾器 @pytest.xfail()、@pytest.skipif()
import pytest

# 當用例通過時标注為 xfail
@pytest.mark.xfail(condition=lambda: True, reason='這是一個預期失敗的用例')
def test_xfail_expected_failure():
    """this test is an xfail that will be marked as expected failure"""
    assert False

# 當用例通過時标注為 xpass
@pytest.mark.xfail
def test_xfail_unexpected_pass():
    """this test is an xfail that will be marked as unexpected success"""
    assert True

# 跳過用例
@pytest.mark.skipif('2 + 2 != 5', reason='當條件觸發時這個用例被跳過 @pytest.mark.skipif')
def test_skip_by_triggered_condition():
    pass           
軟體測試丨Allure2報告中添加用例支援tags标簽、失敗重試功能

Allure2 添加用例标簽-fixture(終結器)

  • 應用場景:fixture 和 finalizer 是分别在測試開始之前和測試結束之後由 Pytest 調用的實用程式函數。Allure 跟蹤每個 fixture 的調用,并詳細顯示調用了哪些方法以及哪些參數,進而保持了調用的正确順序。
"""
@Author: 霍格沃茲測試開發學社-西西
@Desc: 更多測試開發技術探讨,請通路:https://ceshiren.com/t/topic/15860
"""
import pytest

@pytest.fixture()

def func(request):
    print("這是一個fixture方法")

    # 定義一個終結器,teardown動作放在終結器中
    def over():
        print("session級别終結器")

    request.addfinalizer(over)


class TestClass(object):
    def test_with_scoped_finalizers(self,func):
        print("測試用例")           
import pytest

@pytest.fixture()
def func1():
    print("這是fixture func1 前置動作")
    yield
    print("這是fixture func1 後置動作")
@pytest.fixture()
def func(request):
    # 前置動作 -- 相當于setup
    print("這是一個fixture方法")
    # 後置動作 -- 相當于teardown
    # 定義一個終結器,teardown動作放在終結器中
    def over():
        print("session級别終結器")
    # 添加終結器,在執行完測試用例之後會執行終結器中的内容
    request.addfinalizer(over)


class TestClass(object):
    def test_with_scoped_finalizers(self,func,func1):
        print("測試用例")           
軟體測試丨Allure2報告中添加用例支援tags标簽、失敗重試功能

在setup中後執行的,在teardown中先執行

2、Allure2報告中支援記錄失敗重試功能

Allure2 失敗重試功能應用場景

  • Allure 可以收集用例運作期間,重試的用例的結果,以及這段時間重試的曆史記錄。
軟體測試丨Allure2報告中添加用例支援tags标簽、失敗重試功能

Allure2 失敗重試功能

  • 重試功能可以使用 pytest 相關的插件,例如 pytest-rerunfailures。
  • 重試的結果資訊,會展示在詳情頁面的”Retries” 頁籤中。
"""
@Author: 霍格沃茲測試開發學社-西西
@Desc: 更多測試開發技術探讨,請通路:https://ceshiren.com/t/topic/15860
"""
import pytest

@pytest.mark.flaky(reruns=2, reruns_delay=2)
def test_rerun2():
    assert False           
軟體測試丨Allure2報告中添加用例支援tags标簽、失敗重試功能
軟體測試丨Allure2報告中添加用例支援tags标簽、失敗重試功能