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

相关推荐
lucia_zl2 天前
pytest并发测试,资源问题导致用例失败解决办法
pytest
鱼鱼说测试2 天前
Selenium4+Pytest自动化测试框架实战
pytest
XYiFfang8 天前
【Pytest】解决Pytest中Teardown钩子的TypeError:实例方法与类方法的调用差异
python·pytest
Kingairy10 天前
Pytest 插件:pytest_runtest_protocol
python·pytest
AIZHINAN10 天前
Pytest 插件介绍和开发
测试工具·pytest·插件开发
灰阳阳12 天前
替身演员的艺术:pytest-mock 从入门到飙戏
自动化测试·python·pytest·unit testing·pytest-mock
年年测试16 天前
Playwright与PyTest结合指南
pytest
focksorCr16 天前
pytest 并发执行用例(基于受限的测试资源)
python·pytest
律品21 天前
pytest的前置与后置
开发语言·python·pytest