首页 > 解决方案 > Pytest 会话夹具不再执行

问题描述

有一段时间,我在最高级别的 conftest.py 中运行了一个带有以下固定装置的测试套件。此文件包含必须可用于测试套件中的每个测试的固定装置。他们正在互相建设。一个夹具需要另一个。因此,执行顺序是隐含明确的。夹具基本上产生连接所需的对象。

@pytest.fixture(scope="session")
def A():
    yield A

@pytest.fixture(scope="session")
def B(A):
    yield B

@pytest.fixture(scope="session")
def C(B):

然后在某一时刻,设置不再起作用。只执行了第一个夹具。

@pytest.fixture(scope="session")
def A():
    yield A

我们目前正在尝试检查可能已更改的确切内容,从而改变了行为。我们尝试更改 pytest 版本,更改 pytest.ini 文件或init .py 文件。到目前为止,我们还没有找到改变行为的任何原因。

有人有提示吗?

标签: pytest

解决方案


添加autouse=True参数使固定装置再次工作。

@pytest.fixture(scope="session", autouse=True)
def A():
    yield A

@pytest.fixture(scope="session", autouse=True)
def B(A):
    yield B

@pytest.fixture(scope="session", autouse=True)
def C(B):

推荐阅读