pytest-fixture固件的使用

**fixture 固件:pytest自动化中用于前后置,功能强大,可替代setup,teardown,setup_class,teardown_class

装饰器:
@pytest.fixture(scope="作用域", params="参数化", autouse="自动执行", ids="参数别名", name="别名"**

下面是fixture的两种用法:

1.fixture固件的自动调用

1)@pytest.fixture(scope='class',params=['name1','name2'],autouse=True)

def login(request):

print("用户登录")

yield reqyest.param

print("退出登录")

2)类中的每个用例都会自动调用login

2.fixture固件的手动调用

1)@pytest.fixture(scope='function',autouse=False)

def login():

print("用户登录")

yield #启用后置

print("退出登录")

2)测试用例中手动调用

def test_001(self, login):

只有指定的如上述调用了fixture函数的测试用例才会生效

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