1. 程式人生 > 實用技巧 >pytest(三十二)--自定義用例順序(pytest-ordering)

pytest(三十二)--自定義用例順序(pytest-ordering)

前言

測試用例在設計的時候,我們一般要求不要有先後順序,用例是可以打亂了執行的,這樣才能達到測試的效果。

有些同學在寫用例的時候,用例寫了先後順序,有先後順序後,後面還會有新的問題(如:上個用例返回資料作為下個用例傳參,等等一系列的問題)

github上有個pytest-ordering外掛可以控制用例的執行順序,github外掛地址https://github.com/ftobia/pytest-ordering

環境準備

先安裝依賴包

pip install pytest-ordering

使用案例

先看pytest預設的執行順序,是按test_a.py檔案寫的用例先後順序執行的。

#test_a.py
import pytest
def test_a():
    print("用例111")
    assert True
def test_b():
    print("用例222")
    assert True
def test_c():
    print("用例333")
    assert True  

 執行結果

使用pytest-ordering外掛後改變測試用例順序

#test_a.py
import pytest
@pytest.mark.run(order=2)
def test_a():
    print("用例111")
    assert True

@pytest.mark.run(order=1)
def test_b():
    print("用例222")
    assert True

@pytest.mark.run(order=3)
def test_c():
    print("用例333")
    assert True

 執行結果

這樣就是按指定的順序執行的用例