两个函数作用域装置以相反的顺序运行

问题描述 投票:0回答:1

我有这样的问题,我有一个名为 appclient 的固定装置,它是会话范围,它的主体如下所示:

@pytest.fixture(scope='session') def appclient(): yield SomeClass()

然后我有两个固定装置要在测试后清理,

cleanup_1
cleanup_2
,两者都设置为“功能”范围,并且都继承自appclient固定装置并具有如下主体:

@pytest.fixture(scope='function')
    def cleanup_1(appclient):
    yield
    <do_something>

我将它们全部导入到测试功能(我不使用 conftest 来测试这个装置,因为我那里有太多)并使用它们,如下所示:

def test_1(appclient, cleanup_1, cleanup_2)
,我的问题是
cleanup_2
总是在
cleanup_1
之前完成,我不明白为什么。只有当我将
cleanup_2
范围设置为模块时,它才开始工作。

我试图检查字母顺序是否是一个问题或一些依赖关系,但似乎不是,我也不明白为什么这个装置似乎用于设置以及yield语句之上没有任何内容。

python testing pytest fixtures
1个回答
0
投票

这里最简单的解决方案是确保

cleanup_2
取决于
cleanup_1
--

@pytest.fixture
def cleanup_2(appclient, cleanup_1):
    yield
© www.soinside.com 2019 - 2024. All rights reserved.