亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

pytest多文件執(zhí)行順序控制詳解

 更新時間:2022年07月05日 09:49:31   作者:FatPuffer  
默認(rèn)情況下pytest測試用例的執(zhí)行順序是先按照外層后內(nèi)層(目錄下的文件),同層級的包或文件、根據(jù)名稱、按照ascii碼升序執(zhí)行,文件內(nèi)的用例根據(jù)先后順序執(zhí)行,這篇文章主要給大家介紹了關(guān)于pytest多文件執(zhí)行順序控制的相關(guān)資料,需要的朋友可以參考下

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,保證測試用例順序為:用戶模塊-->訂單模塊-->庫存模塊

方式一:通過修改文件名,使得文件名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ù)模塊去命名的,所以通過修改文件名實現(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)文章

  • python編寫一個GUI倒計時器

    python編寫一個GUI倒計時器

    這篇文章主要為大家詳細(xì)介紹了python編寫一個GUI倒計時器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-08-08
  • tensorflow 恢復(fù)指定層與不同層指定不同學(xué)習(xí)率的方法

    tensorflow 恢復(fù)指定層與不同層指定不同學(xué)習(xí)率的方法

    今天小編就為大家分享一篇tensorflow 恢復(fù)指定層與不同層指定不同學(xué)習(xí)率的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-07-07
  • python tkinter庫實現(xiàn)氣泡屏保和鎖屏

    python tkinter庫實現(xiàn)氣泡屏保和鎖屏

    這篇文章主要為大家詳細(xì)介紹了python tkinter庫實現(xiàn)氣泡屏保和鎖屏,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-07-07
  • 寫一個Python腳本下載嗶哩嗶哩舞蹈區(qū)的所有視頻

    寫一個Python腳本下載嗶哩嗶哩舞蹈區(qū)的所有視頻

    B 站大家都熟悉,尤其是它的舞蹈區(qū).有 100W+ 的舞蹈視頻.在沒有 wifi 的情況下,就欣賞不了這些視頻了.作為一個 python 程序員,小編就寫一個 Python 腳本在晚上下載舞蹈區(qū)的所有視頻,需要的朋友可以參考下
    2021-05-05
  • Python里字典的基本用法(包括嵌套字典)

    Python里字典的基本用法(包括嵌套字典)

    今天小編就為大家分享一篇關(guān)于Python里字典的基本用法(包括嵌套字典),小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-02-02
  • PyOD進(jìn)行異常值檢測使用實例

    PyOD進(jìn)行異常值檢測使用實例

    異常值檢測各個領(lǐng)域的關(guān)鍵任務(wù)之一,PyOD是Python Outlier Detection的縮寫,可以簡化多變量數(shù)據(jù)集中識別異常值的過程,在本文中,我們將介紹PyOD包,并通過實際給出詳細(xì)的代碼示例
    2024-02-02
  • python Dtale庫交互式數(shù)據(jù)探索分析和可視化界面

    python Dtale庫交互式數(shù)據(jù)探索分析和可視化界面

    這篇文章主要為大家介紹了python Dtale庫交互式數(shù)據(jù)探索分析和可視化界面實現(xiàn)功能詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2024-01-01
  • 利用python判斷字母大小寫的幾種方法小結(jié)

    利用python判斷字母大小寫的幾種方法小結(jié)

    在開發(fā)過程中有時候我們需要判斷一個字符串是否是小寫形式,下面這篇文章主要給大家介紹了關(guān)于利用python判斷字母大小寫的幾種方法,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-05-05
  • python基礎(chǔ)教程之Hello World!

    python基礎(chǔ)教程之Hello World!

    這篇文章主要介紹了python基礎(chǔ)教程之Hello World!,本文講解了命令行中、文件中、腳本文件中輸出Hello World的例子,需要的朋友可以參考下
    2014-08-08
  • python關(guān)于os.walk函數(shù)查找windows文件方式

    python關(guān)于os.walk函數(shù)查找windows文件方式

    這篇文章主要介紹了python關(guān)于os.walk函數(shù)查找windows文件方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-08-08

最新評論