python - Pytest - 从另一个夹具调用夹具
问题描述
我有一个夹具,它返回某种类型的对象,我在另一个文件中定义了另一个夹具,它基本上使用该对象来做其他事情。但是我无法从我的第一个夹具中返回对象。
file-1
def fixture_1(s, **kwargs):
def hook(s, **kwargs):
p_b = s.get()
p = p_b.build()
yield p
return hook
file-2
conftest.py
@pytest.fixture(scope='module')
def fixture_1(s, **kwargs):
def hook(s, **kwargs):
#Default implementation is no-op.
pass
return hook
@pytest.fixture(scope='module')
def fixture_2(s,b_p):
some_p = fixture_1(s)
current_status = s.start(some_p)
print(current_status)
yield current_status
我想基本上检索p
返回的对象并在夹具中file-1
fixture_1
使用它。file-2
fixture_2
解决方案
看来您使用 pytest 固定装置错误(查看您的参数名称)。
我建议您通过https://docs.pytest.org/en/latest/fixture.html
您的问题有两种解决方案:
###
# file_1
def not_really_a_fixture(s, **kwargs): # just some hook generator
def hook(s, **kwargs):
p_b = s.get()
p = p_b.build()
yield p
return hook
###
# conftest.py
from file_1 import not_really_a_fixture
@pytest.fixture(scope='module')
def fixture_2(s,b_p): # s and b_p should be names of fixtures that need to run before this
some_p = not_really_a_fixture(s)
current_status = s.start(some_p)
print(current_status)
yield current_status
和:
###
# file_1
@pytest.fixture(scope='module')
def fixture_1(s): # s is name of another fixture
# there is no point in having **kwargs as arg in pytest fixture
def hook(s, **kwargs):
#Default implementation is no-op.
pass
return hook
###
# conftest.py
from file_1 import fixture_1
@pytest.fixture(scope='module')
def fixture_2(s,b_p,fixture_1): # s and b_p should be names of fixtures that need to run before this
# in fixture_1 is value returned by fixture_1, that means your hook func
current_status = s.start(fixture_1)
print(current_status)
yield current_status
推荐阅读
- python - 如何在 DataFrame 中按序列分组
- flutter - Flutter - 根据可用空间动态半径的 CircleAvatar
- python - 打印数据框时去掉引号
- javascript - 设置范围滑块的最小处理程序值
- python - 使用 AWS CDK 创建受 MFA 保护的角色会绕过 MFA 条件
- twitter-bootstrap - 为什么升级到 Bootstrap 4 后我的列表现不同?
- python - 如何祝贺玩家使用提示而不以不同方式使用它
- c# - 用图像词替换文本
- c# - NUnit 3 动态 ValueSourceAttribute
- dns - Plesk 自动续订 Letencrypt 使用 Google Cloud DNS