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函数的测试用例才会生效

相关推荐
一个处女座的测试2 天前
Python语言+pytest框架+allure报告+log日志+yaml文件+mysql断言实现接口自动化框架
python·mysql·pytest
思则变5 天前
[Pytest][Part 1]Pytest 自动化测试框架
pytest
思则变6 天前
[Pytest] [Part 2]增加 log功能
开发语言·python·pytest
思则变7 天前
[Pytest][Part 3]检测python package状态
pytest
cooldream200921 天前
pytest 框架详解与实战指南
pytest·测试
慕城南风21 天前
【pytest进阶】Pytest之conftest详解
pytest
编程小白gogogo22 天前
AI自动化测试速成(Pytest框架)
pytest
慕城南风22 天前
【pytest进阶】pytest详解及进阶使用
linux·服务器·pytest
Tom Boom1 个月前
Pytest断言全解析:掌握测试验证的核心艺术
自动化测试·python·测试开发·pytest