前言
測試用例在設計的時候,我們一般要求不要有先后順序,用例是可以打亂了執行的,這樣才能達到測試的效果.
有些同學在寫用例的時候,用例寫了先后順序, 有先后順序后,后面還會有新的問題(如:上個用例返回數據作為下個用例傳參,等等一系列的問題。。。)
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 ===========================
這樣就是按指定的順序執行的用例