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。

网易云完整视频课程《pytest+yaml 框架使用与开发》https://study.163.com/course/courseMain.htm?courseId=1213419817&share=2&shareId=480000002230338

posted @   上海-悠悠  阅读(1318)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示