python接口自動(dòng)化-pytest-控制用例執(zhí)行順序pytest-ordering

# -*- coding: utf-8 -*-
# @Time    : 2023/6/27 10:05
# @Author  : yanfa
# @user   : yanfa 
# @File    : test_pytest_for_ordering.py
# @remark: 自定義用例執(zhí)行順序

""""""
import pytest

"""一、pytest常用插件
1、pip install pytest-ordering 控制用例執(zhí)行順序
2、pip install pytest-xdist 分布式并發(fā)執(zhí)行測試用例
3、pip install pytest-dependency 控制用例的依賴關(guān)系
4、pip install pytest-rerunfailures 失敗重試
5、pip install pytest-assume 多重校驗(yàn)
6、pip install pytest-random-order 用例隨機(jī)執(zhí)行
7、pip install pytest-html 測試報(bào)告
"""

"""二、pytest執(zhí)行順序
場景:
對(duì)于集成測試,經(jīng)常會(huì)有上下文依賴關(guān)系的測試用例。
比如10個(gè)步驟,拆成10條case,這時(shí)候能知道到底執(zhí)行到哪步報(bào)錯(cuò)。,
而用例默認(rèn)執(zhí)行順序是自上而下,需要控制執(zhí)行順序

解決:
可以通過setup/teardown/fixture來解決,也可以使用對(duì)應(yīng)插件。

安裝:pip install pytest-ordering (倉庫地址https://github.com/ftobia/pytest-ordering)

用法:
    @pytest.mark.first 對(duì)應(yīng)0,不推薦這種寫法
    @pytest.mark.last 對(duì)應(yīng)-1,不推薦這種寫法
    @pytest.mark.run(order=0) 對(duì)應(yīng)第一個(gè),推薦這種寫法
    @pytest.mark.run(order=2) 對(duì)應(yīng)第三個(gè),推薦這種寫法

注意:
    1、多個(gè)插件裝飾器(>2)的時(shí)候,有可能會(huì)發(fā)生沖突
    2、order支持英文/正整數(shù)/負(fù)整數(shù),執(zhí)行優(yōu)先級(jí):0>較小的正數(shù)>較大的正數(shù)>無標(biāo)記>較小的負(fù)數(shù)>較大的負(fù)數(shù)
    orders_map = {
    'first': 0,
    'second': 1,
    'third': 2,
    'fourth': 3,
    'fifth': 4,
    'sixth': 5,
    'seventh': 6,
    'eighth': 7,
    'last': -1,
    'second_to_last': -2,
    'third_to_last': -3,
    'fourth_to_last': -4,
    'fifth_to_last': -5,
    'sixth_to_last': -6,
    'seventh_to_last': -7,
    'eighth_to_last': -8,
}
"""
#例子 執(zhí)行順序0->2->4->無標(biāo)記->-4->-2 (0>較小的正數(shù)>較大的正數(shù)>無標(biāo)記>較小的負(fù)數(shù)>較大的負(fù)數(shù))
@pytest.mark.run(order=4)
def test_demo1():
    print("4")

@pytest.mark.run(order=2)
def test_demo2():
    print("2")

@pytest.mark.run(order=0)
def test_demo3():
    print("0")

def test_demo4():
    print("無標(biāo)記")

@pytest.mark.run(order=-4)
def test_demo5():
    print("-4")

@pytest.mark.run(order=-2)
def test_demo6():
    print("-2")

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容