pytest文檔52-命令行參數--setup-show查看fixture的執行過程


前言

使用命令行運行 pytest 用例的時候,看不到 fixture 的執行過程.
如果我們想知道fixture的執行過程和先后順序,可以加上 --setup-show 命令行參數,幫助查看 fixture 的執行過程.

--setup-show

案例參考test_s.py

# test_s.py
import pytest
# 作者-上海悠悠 QQ交流群:717225969
# blog地址 https://www.cnblogs.com/yoyoketang/


@pytest.fixture()
def login():
    print("前置操作:准備數據")
    yield
    print("后置操作:清理數據")


def test_01(login):
    a = "hello"
    b = "hello"
    assert a == b


def test_02(login):
    a = "hello"
    b = "hello world"
    assert a in b

命令行執行 pytest test_s.py

>pytest test_s.py
============================= test session starts =============================

collected 2 items

test_s.py ..                                                             [100%]

========================== 2 passed in 0.10 seconds ===========================

加上 --setup-show 命令行參數后執行

>pytest test_s.py --setup-show
============================= test session starts =============================
collected 2 items

test_s.py
SETUP    S base_url
SETUP    S _verify_url (fixtures used: base_url)
        SETUP    F __pytest_repeat_step_number
        SETUP    F login
        test_s.py::test_01 (fixtures used: __pytest_repeat_step_number, _verify_url, base_url, login).
        TEARDOWN F login
        TEARDOWN F __pytest_repeat_step_number
        SETUP    F __pytest_repeat_step_number
        SETUP    F login
        test_s.py::test_02 (fixtures used: __pytest_repeat_step_number, _verify_url, base_url, login).
        TEARDOWN F login
        TEARDOWN F __pytest_repeat_step_number
TEARDOWN S _verify_url
TEARDOWN S base_url

========================== 2 passed in 0.04 seconds ===========================

這樣就可以方便查看用例調用了哪些fixture,上面用例里面只寫了一個login
但是從回溯信息上看到還有幾個是內置的fixture會自動調用:__pytest_repeat_step_number, _verify_url, base_url。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM