pytest結合allure添加測試用例步驟(allure.step)


  在編寫自動化測試用例的時候經常會遇到需要編寫流程性測試用例的場景,一般流程性的測試用例的測試步驟比較多,我們在測試用例中添加詳細的步驟會提高測試用例的可閱讀性。allure提供的裝飾器@allure.step()是allure測試報告框架非常有用的功能,它能幫助我們在測試用例中對測試步驟進行詳細的描述。

@allure.step的使用例子:

  實現一個購物的場景:1.登錄;2.瀏覽商品;3.將商品加入到購物車中;4.下單;5.支付訂單;

# file_name: test_allure_step.py


import pytest
import allure


@allure.step
def login():
    """
    執行登錄邏輯
    :return:
    """
    print("執行登錄邏輯")


@allure.step
def scan_good():
    """
    執行瀏覽商品邏輯
    :return:
    """
    print("執行瀏覽商品邏輯")


@allure.step
def add_good_to_shopping_car():
    """
    將商品添加到購物車
    :return:
    """
    print("將商品添加到購物車")


@allure.step
def generator_order():
    """
    生成訂單
    :return:
    """
    print("生成訂單")


@allure.step
def pay():
    """
    支付訂單
    :return:
    """
    print("支付訂單")


def test_buy_good():
    """
    測試購買商品:
    步驟1:登錄
    步驟2:瀏覽商品
    步驟3:將商品加入到購物車中
    步驟4:下單
    步驟5:支付
    :return:
    """
    login()
    scan_good()
    add_good_to_shopping_car()
    generator_order()
    pay()

    with allure.step("斷言"):
        assert 1


if __name__ == '__main__':
    pytest.main(['-s', 'test_allure_step.py'])

執行命令:

> pytest test_allure_step.py --alluredir=./report/result_data

> allure serve ./report/result_data

查看測試報告展示效果:

  從報告中可以看到,我們事先通過@allure.step()定義好的步驟都展示在測試用例test_buy_good()下了。

@allure.step支持嵌套,step中調用step:

# file_name: steps.py


import allure


@allure.step
def passing_step_02():
    print("執行步驟02")
    pass

測試用例:

# file_name: test_allure_step_nested.py


import pytest
import allure

from .steps import passing_step_02  # 從外部模塊中導入


@allure.step
def passing_step_01():
    print("執行步驟01")
    pass


@allure.step
def step_with_nested_steps():
    """
    這個步驟中調用nested_step()
    :return:
    """
    nested_step()


@allure.step
def nested_step():
    """
    這個步驟中調用nested_step_with_arguments(),並且傳遞參數
    :return:
    """
    nested_step_with_arguments(1, 'abc')


@allure.step
def nested_step_with_arguments(arg1, arg2):
    pass


def test_with_imported_step():
    """
    測試@allure.step()支持調用從外部模塊導入的step
    :return:
    """
    passing_step_01()
    passing_step_02()


def test_with_nested_steps():
    """
    測試@allure.step()支持嵌套調用step
    :return:
    """
    passing_step_01()
    step_with_nested_steps()
    passing_step_02()


if __name__ == '__main__':
    pytest.main(['-s', 'test_allure_step_nested.py'])

執行命令:

pytest test_allure_step_nested.py --alluredir=./report/result_data

allure serve ./report/result_data

查看測試報告展示效果:

  從上面的結果中可以看到:

  • @step可以先保存到其他模塊中,在測試用例中需要用到的時候導入就可以了;
  • @step也支持在一個step中嵌套調用其他的step;嵌套的形式在測試報告中以樹形展示出來了;

@allure.step支持添加描述且通過占位符傳遞參數:

# file_name: test_allure_step_with_placeholder.py


import pytest
import allure


@allure.step('這是一個帶描述語的step,並且通過占位符傳遞參數:positional = "{0}",keyword = "{key}"')
def step_title_with_placeholder(arg1, key=None):
    pass


def test_step_with_placeholder():
    step_title_with_placeholder(1, key="something")
    step_title_with_placeholder(2)
    step_title_with_placeholder(3, key="anything")


if __name__ == '__main__':
    pytest.main(['-s', 'test_allure_step_with_placeholder.py'])

執行命令:

pytest test_allure_step_with_placeholder.py --alluredir=./report/result_data

allure serve ./report/result_data

查看測試報告展示效果:

  從上面的執行結果中可以看到,@allure.step()是支持輸入描述的,並且支持通過占位符向描述中傳遞參數。

 在conftest.py文件中定義@allure.step:

conftest.py文件:

# file_name: conftest.py


import pytest
import allure


@pytest.fixture()
def fixture_with_conftest_step():
    conftest_step()


@allure.step("這是一個在conftest.py文件中的step")
def conftest_step():
    pass

測試用例:

# file_name: test_allure_step_in_fixture_from_conftest.py


import pytest
import allure


@allure.step
def passed_step():
    pass


def test_with_step_in_fixture_from_conftest(fixture_with_conftest_step):
    passed_step()


if __name__ == '__main__':
    pytest.main(['-s', 'test_allure_step_in_fixture_from_conftest.py'])

執行命令:

pytest test_allure_step_in_fixture_from_conftest.py --alluredir=./report/result_data

allure serve ./report/result_data

查看測試報告展示效果:

  從運行結果中可以看到,在fixture中定義的step會在setup和teardown單獨以樹形結構展示出來。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM