pytest fixture夹具,@pytest.fixture

fixture 是pytest 用于测试前后进行预备,清理工作的代码处理机制

fixture相对于setup 和teardown:

fixure ,命名更加灵活,局限性比较小

conftest.py 配置里面可以实现数据共享,不需要import 就能自动找到一些配置

setup / teardown

模块级 setup_module / teardown_module 开始于模块始末,生效一次

函数级 setup_function / teardown_function 对每条函数用例生效 (不在类中)

类级 setup_class / teardown_class 只在类中前后运行一次 (在类中)

方法级 setup_method / teardown_method 开始于方法始末 (在类中)

(scop="function") 每一个函数或方法都会调用

(scop="class") 每一个类调用一次

(scop="module") 每一个.py文件调用一次

(scop="session") 是多个文件调用一次,.py文件就是module

fixture的作用范围:session>module>class>function

相关推荐
姚青&10 天前
PyCharm 配置与界面化运行
pytest
姚青&10 天前
Pytest 测试用例断言
测试用例·pytest
姚青&10 天前
Pytest 命名规则
pytest
姚青&10 天前
Pytest 测试用例结构
测试用例·pytest
姚青&10 天前
Pytest 简介、安装与环境准备
pytest
测试渣11 天前
持续集成中的自动化测试框架优化实战指南
python·ci/cd·单元测试·自动化·pytest
小凯1234511 天前
pytest框架-详解(学习pytest框架这一篇就够了)
python·学习·pytest
逻极11 天前
pytest 入门指南:Python 测试框架从零到一(2025 实战版)
开发语言·python·pytest