pytest多文件執(zhí)行順序控制詳解
1.只有一個py文件
1.使用pytest做接口測試,如果測試case只存在于單個.py文件,那么測試case默認(rèn)從上到下執(zhí)行,如果使用了pytest-order插件
2.如果存在多個py文件
1.使用pytest做接口測試,如果測試case存在于多個.py文件中,那么默認(rèn)是按照文件名的ascii碼順序執(zhí)行,進(jìn)入文件后,默認(rèn)按照從上到下順序執(zhí)行每個單元測試接口。
test_user.py # 用戶相關(guān)
class TestUser:
def test_user_create:
def test_user_login:
def test_user_delete
test_order.py # 訂單相關(guān)
class TestOrder:
def test_order_create:
def test_order_list:
def test_order_delete
test_stock.py # 庫存相關(guān)
class TestStock:
def test_stock_add:
def test_stock_list:
def test_stock_reduce
1.按照文件名ascii排序:test_order > test_stock > test_user
2.test_order_create > test_order_list > test_order_delete > test_stock_add > test_stock_list > …
2.如果單個.py測試文件中使用了pytest-order插件,那么該文件中添加了order的測試用例將會最先執(zhí)行,沒添加的將會按照1的順序執(zhí)行,這樣就會出現(xiàn)單元測試的順序在多文件中交叉執(zhí)行的現(xiàn)象。(所以單個.py文件在使用pytest-order插件的情況下,建議每個case都帶上order=x,且x不要相同)
test_user.py # 用戶相關(guān)
class TestUser:
@pytest.mark.run(order=1)
def test_user_create:
def test_user_login:
@pytest.mark.run(order=2)
def test_user_delete
test_order.py # 訂單相關(guān)
class TestOrder:
def test_order_create:
def test_order_list:
def test_order_delete
test_stock.py # 庫存相關(guān)
class TestStock:
def test_stock_add:
def test_stock_list:
def test_stock_reduce
1.由于 test_user 文件中的 case 使用了 pytest-order 插件,所以優(yōu)先執(zhí)行使用了order排序的 case
2.test_user_create > test_user_delete> test_order_create> … > test_stock_add > … > test_user_delete
3.如果多個.py文件使用了pytest-order插件,如果每個order指定的順序不沖突,就按照order指定的順序執(zhí)行,如果有沖突,那就會出現(xiàn)在多個.py文件中交叉執(zhí)行,可能不符合我們預(yù)期。
test_user.py # 用戶相關(guān)
class TestUser:
@pytest.mark.run(order=1)
def test_user_create:
def test_user_login:
@pytest.mark.run(order=2)
def test_user_delete
test_order.py # 訂單相關(guān)
class TestOrder:
def test_order_create:
def test_order_list:
def test_order_delete
test_stock.py # 庫存相關(guān)
class TestStock:
@pytest.mark.run(order=1)
def test_stock_add:
@pytest.mark.run(order=2)
def test_stock_list:
def test_stock_reduce
1.test_stock 和 test_user 存在 order 沖突,所以按照文件名ascii順序排序
2.test_stock_add > test_user_create > test_stock_list > test_user_delete > order相關(guān) > test_stock_reduce > test_user_login
4.多個py文件修改按照文件名ascii碼排序方式
需求:不要再多個文件中來回執(zhí)行case,保證測試用例順序?yàn)椋河脩裟K-->訂單模塊-->庫存模塊
方式一:通過修改文件名,使得文件名ascii碼排序,和我們測試case執(zhí)行順序一致,確保case中沒有pytest-order插件
test_1_user.py # 用戶相關(guān)
class TestUser:
def test_user_create:
def test_user_login:
def test_user_delete
test_2_order.py # 訂單相關(guān)
class TestOrder:
def test_order_create:
def test_order_list:
def test_order_delete
test_3_stock.py # 庫存相關(guān)
class TestStock:
def test_stock_add:
def test_stock_list:
def test_stock_reduce
但通常情況下,我們.py文件是根據(jù)模塊去命名的,所以通過修改文件名實(shí)現(xiàn)我們預(yù)期的執(zhí)行順序,并不是很友好
方式二:如果使用pytest-order插件來控制,必須保證每個文件的order值是不能重復(fù)的,后一個.py文件order最小值必須大于前一個.py文件最大值,這樣就可以確保文件執(zhí)行順序
這樣在增加測試用例后,就可能需要修改很多order順序
test_user.py # 用戶相關(guān)
class TestUser:
@pytest.mark.run(order=1)
def test_user_create:
@pytest.mark.run(order=3)
def test_user_login:
@pytest.mark.run(order=2)
def test_user_delete
test_order.py # 訂單相關(guān)
class TestOrder:
@pytest.mark.run(order=4)
def test_order_create:
@pytest.mark.run(order=5)
def test_order_list:
@pytest.mark.run(order=6)
def test_order_delete
test_stock.py # 庫存相關(guān)
class TestStock:
@pytest.mark.run(order=7)
def test_stock_add:
@pytest.mark.run(order=8)
def test_stock_list:
@pytest.mark.run(order=9)
def test_stock_reduce
方式三:通過pytest提供的勾子方法pytest_collection_modifyitems,對case執(zhí)行順序進(jìn)行修改
# conftest.py
def pytest_collection_modifyitems(config, items)
# 期望用例順序按照.py文件執(zhí)行
appoint_classes = {"TestUser": [], "TestOrder": [], "TestStock": []}
for item in items:
for cls_name in appoint_classes:
if item.parent.name == cls_name:
appoint_classes[cls_name].append(item)
items.clear()
for cases in appoint_classes.values():
items.extend(cases)
用戶只需要將其新增的測試模塊class按照預(yù)期的順序添加到appoint_classes中即可,簡單靈活
總結(jié)
到此這篇關(guān)于pytest多文件執(zhí)行順序控制的文章就介紹到這了,更多相關(guān)pytest多文件執(zhí)行順序內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
tensorflow 恢復(fù)指定層與不同層指定不同學(xué)習(xí)率的方法
今天小編就為大家分享一篇tensorflow 恢復(fù)指定層與不同層指定不同學(xué)習(xí)率的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-07-07
python tkinter庫實(shí)現(xiàn)氣泡屏保和鎖屏
這篇文章主要為大家詳細(xì)介紹了python tkinter庫實(shí)現(xiàn)氣泡屏保和鎖屏,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-07-07
python Dtale庫交互式數(shù)據(jù)探索分析和可視化界面
這篇文章主要為大家介紹了python Dtale庫交互式數(shù)據(jù)探索分析和可視化界面實(shí)現(xiàn)功能詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01
python關(guān)于os.walk函數(shù)查找windows文件方式
這篇文章主要介紹了python關(guān)于os.walk函數(shù)查找windows文件方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-08-08

