pytest扫盲7--fixture之 autouse 参数
1、源码解释如下:
:arg autouse: if True, the fixture func is activated for all tests that can see it. If False (the default) then an explicit reference is needed to activate the fixture.
# autouse=True 时,自动使用 fixture
# autouse=False 时,则调用函数名来实现 fixture
2、调用 fixture 的三种方式
- 函数内直接传 fixture 的函数参数名称
- 使用装饰器 @pytest.mark.usefixtures('func_name')
- 定义fixture参数 autouse=True
3、autouse=True 自动使用 fixture(在conftest中编辑,作用范围为在同一级目录中全部自动调用):
# File : conftest.py # IDE : PyCharm import pytest @pytest.fixture(autouse=True) def automaticallyUs(): print('\n实例化webdriver') yield print('\n关闭webdriver')
在同一级目录中新建 .py 文件:
# File : test_demo_9.py # IDE : PyCharm def test_1(): print('\n打开百度...') def test_2(): print('\n登录系统...')
运行脚本:
E:\personal\python38\python.exe E:/personal/GitWorkSpace/pytest_basic/main.py 实例化webdriver 打开百度... . 关闭webdriver 实例化webdriver 登录系统... . 关闭webdriver 2 passed in 0.07s
喜时之言,多失信;怒时之言,多失体
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步