gpt4 book ai didi

python - Pytest - 从另一个装置调用一个装置

转载 作者:行者123 更新时间:2023-12-01 07:47:26 26 4
gpt4 key购买 nike

我有一个返回某种类型对象的固定装置,并且在另一个文件中定义了另一个固定装置,该文件基本上使用该对象来做其他事情。但我无法从我的第一个装置返回该对象。

文件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

我基本上想检索 file-1 fixture_1 返回的对象 p 并在 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 - Pytest - 从另一个装置调用一个装置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56402600/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com