Pytest从另一个fixtu调用fixtu

2024-05-13 10:59:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个fixture,它返回一个特定类型的对象,我在另一个文件中定义了另一个fixture,它基本上使用这个对象来做其他事情。但是我不能从我的第一个固定装置返回物体。在

file-1

def fixture_1(s, **kwargs):
    def hook(s, **kwargs):
        p_b = s.get()
        p = p_b.build()
        yield p
    return hook

file-2conftest.py

^{pr2}$

我想基本上检索返回到file-1fixture_1中的对象p,并在file-2fixture_2fixture中使用它。在


Tags: 文件对象build类型get定义defhook
1条回答
网友
1楼 · 发布于 2024-05-13 10:59:06

似乎您错误地使用了pytest fixtures(查看参数名称)

我强烈建议你通过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
###

还有第二个变种

^{pr2}$

相关问题 更多 >