Python - fixture (二)
fixture 的并列与嵌套调用
常用场景:相互依赖的fixture 可能是多个,或提前准备的数据也可能是多个,或数据准备也可能有依赖或先后
并列使用fixture
@pytest.fixture(scope='module')
def open_brower():
print('打开浏览器!')
def test_cakan():
print('case2: 不登录就看')
def test_cart(login):
print('case3: 登录,加入购物车')
def test_soso(open_brower, login):
print('case1: 登录后执行搜索')
执行结果:
test_module\test_fixture_twofixtures-1.py case2: 不登录就看
.这是登录模块
case3: 登录,加入购物车
.打开浏览器!
这是登录模块
case1: 登录后执行搜索
.
嵌套调用fixture
import pytest
@pytest.fixture()
def login(open_brower): # 1
print('这是登录模块')
@pytest.fixture(scope='module')
def open_brower():
print('打开浏览器!')
def test_cakan():
print('case2: 不登录就看')
def test_cart(login): # 2
print('case3: 登录,加入购物车')
def test_soso(login):
print('case1: 登录后执行搜索')
- 嵌套了 open_brower
- 测试用例在使用的时候可以直接使用login,执行login 之前会先调用 open_brower
执行结果:
test_module\test_fixture_twofixtures-1.py case2: 不登录就看
.打开浏览器!
这是登录模块
case3: 登录,加入购物车
.这是登录模块
case1: 登录后执行搜索
.
嵌套调用获取 fixture 返回值
@pytest.fixture()
def login(open_brower):
print(f'open_brower 返回值:{open_brower}')
print('这是登录模块')
@pytest.fixture(scope='module')
def open_brower():
print('打开浏览器!')
return 1
def test_soso(login):
print('case1: 登录后执行搜索')
执行结果:
test_module\test_fixture_twofixtures-1.py 打开浏览器!
open_brower 返回值:1
这是登录模块
case1: 登录后执行搜索
.
本文来自博客园,作者:chuangzhou,转载请注明原文链接:https://www.cnblogs.com/czzz/p/18267858
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
2022-06-25 HTTP - 缓存