pytest文檔37-自定義用例順序(pytest-ordering)


前言

測試用例在設計的時候,我們一般要求不要有先后順序,用例是可以打亂了執行的,這樣才能達到測試的效果.
有些同學在寫用例的時候,用例寫了先后順序, 有先后順序后,后面還會有新的問題(如:上個用例返回數據作為下個用例傳參,等等一系列的問題。。。)
github 上有個 pytest-ordering 插件可以控制用例的執行順序,github插件地址https://github.com/ftobia/pytest-ordering

環境准備

先安裝依賴包

pip install pytest-ordering

使用案例

先看pytest默認的執行順序,是按 test_ording.py 文件寫的用例先后順序執行的

# test_ording.py
import pytest
# 上海-悠悠


def test_foo():
    print("用例11111111111")
    assert True


def test_bar():
    print("用例22222222222")
    assert True


def test_g():
    print("用例333333333333333")
    assert True

運行結果

D:\demo>pytest test_ording.py -vs
============================= test session starts =============================
platform win32 -- Python 3.6.0
cachedir: .pytest_cache
metadata: 
plugins: ordering-0.6,
collected 3 items

test_ording.py::test_foo 用例11111111111
PASSED
test_ording.py::test_bar 用例22222222222
PASSED
test_ording.py::test_g 用例333333333333333
PASSED

========================== 3 passed in 0.07 seconds ===========================

使用 pytest-ordering 插件后改變測試用例順序

# test_ording.py
import pytest
# 上海-悠悠


@pytest.mark.run(order=2)
def test_foo():
    print("用例11111111111")
    assert True


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


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

運行結果

D:\demo>pytest test_ording.py -vs
============================= test session starts =============================
platform win32 -- Python 3.6.0
cachedir: .pytest_cache
metadata: 
plugins: ordering-0.6,
collected 3 items

test_ording.py::test_bar 用例22222222222
PASSED
test_ording.py::test_foo 用例11111111111
PASSED
test_ording.py::test_g 用例333333333333333
PASSED

========================== 3 passed in 0.04 seconds ===========================

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


免責聲明!

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



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