最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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,保證測試用例順序?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)文章

  • Python中DataFrame轉(zhuǎn)列表的最全指南

    Python中DataFrame轉(zhuǎn)列表的最全指南

    在Python數(shù)據(jù)分析中,Pandas的DataFrame是最常用的數(shù)據(jù)結(jié)構(gòu)之一,本文將為你詳解5種主流DataFrame轉(zhuǎn)換為列表的方法,大家可以根據(jù)需求進(jìn)行選擇
    2025-03-03
  • Python面向?qū)ο笾瓷?自省機(jī)制實(shí)例分析

    Python面向?qū)ο笾瓷?自省機(jī)制實(shí)例分析

    這篇文章主要介紹了Python面向?qū)ο笾瓷?自省機(jī)制,結(jié)合實(shí)例形式分析了Python面向?qū)ο蟪绦蛟O(shè)計(jì)中的反射/自省機(jī)制概念、原理及相關(guān)屬性操作技巧,需要的朋友可以參考下
    2018-08-08
  • Pyecharts之特殊圖表的實(shí)現(xiàn)示例

    Pyecharts之特殊圖表的實(shí)現(xiàn)示例

    本文主要介紹了Pyecharts之特殊圖表的實(shí)現(xiàn)示例,包括象形圖、水球圖和日歷圖的定制方法,具有一定的參考價值,感興趣的可以了解一下
    2025-01-01
  • 詳解python常用命令行選項(xiàng)與環(huán)境變量

    詳解python常用命令行選項(xiàng)與環(huán)境變量

    這篇文章主要介紹了python常用命令行選項(xiàng)與環(huán)境變量,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-02-02
  • Python3中正則模塊re.compile、re.match及re.search函數(shù)用法詳解

    Python3中正則模塊re.compile、re.match及re.search函數(shù)用法詳解

    這篇文章主要介紹了Python3中正則模塊re.compile、re.match及re.search函數(shù)用法,結(jié)合實(shí)例形式較為詳細(xì)的分析了re模塊 中re.compile、re.match及re.search函數(shù)的功能、參數(shù)、具體使用技巧與注意事項(xiàng),需要的朋友可以參考下
    2018-06-06
  • python實(shí)現(xiàn)圖像拼接

    python實(shí)現(xiàn)圖像拼接

    這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)圖像拼接,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-03-03
  • python for循環(huán)輸入一個矩陣的實(shí)例

    python for循環(huán)輸入一個矩陣的實(shí)例

    今天小編就為大家分享一篇python for循環(huán)輸入一個矩陣的實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-11-11
  • Python實(shí)現(xiàn)PDF轉(zhuǎn)Word的多種方式總結(jié)

    Python實(shí)現(xiàn)PDF轉(zhuǎn)Word的多種方式總結(jié)

    這篇文章主要為大家詳細(xì)介紹了三種Python實(shí)現(xiàn)PDF文件轉(zhuǎn)Word文檔的方式,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2023-11-11
  • Python實(shí)現(xiàn)一鍵自動分類管理文件

    Python實(shí)現(xiàn)一鍵自動分類管理文件

    經(jīng)常雜亂無章的文件夾會讓我們找不到所想要的文件,所以本文小編特意為大家介紹了如何制作一個可視化GUI界面,通過輸入路徑一鍵點(diǎn)擊實(shí)現(xiàn)文件分門別類的歸檔,希望對大家有所幫助<BR>
    2024-01-01
  • Python全棧之正則表達(dá)式

    Python全棧之正則表達(dá)式

    這篇文章主要為大家介紹了Python正則表達(dá)式,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2021-11-11

最新評論

牡丹江市| 荥经县| 微博| 郓城县| 玛沁县| 绥棱县| 牡丹江市| 湖口县| 广饶县| 永春县| 临安市| 任丘市| 囊谦县| 通辽市| 定日县| 屏东县| 阿拉尔市| 新巴尔虎左旗| 阿拉善右旗| 文山县| 洛扎县| 金门县| 天镇县| 射洪县| 田林县| 治县。| 义马市| 偃师市| 锡林郭勒盟| 阿克| 锦屏县| 长乐市| 天峻县| 泉州市| 灵台县| 渝北区| 古田县| 澄江县| 宜阳县| 比如县| 鸡泽县|