【问题标题】:Pytest session fixtures not executed anymorePytest 会话夹具不再执行
【发布时间】:2021-09-03 03:46:26
【问题描述】:

一段时间以来,我在最高级别的 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 文件。到目前为止,我们还没有找到改变行为的任何原因。

有人有提示吗?

【问题讨论】:

  • 如何调用fixture?
  • 夹具包含在 conftest.py 文件中。测试使用“pytest testing/”执行,有效地调用了各种测试文件和子文件夹(以及从属 conftest.py 文件中的更多夹具)中的所有测试的执行。
  • 是的,但是您在哪里调用固定装置?它们未设置为autoUse=True,因此必须在某处调用它们。
  • 在排名较低的夹具中,较高顺序的夹具被拾取 @pytest.fixture(scope="session") def justanothersubfixture(B): 最后测试实际上是调用夹具 def test_finaltest( B、justanothersubfixture): #final test

标签: pytest


【解决方案1】:

添加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):

【讨论】:

    猜你喜欢
    • 2023-03-04
    • 1970-01-01
    • 2020-01-16
    • 1970-01-01
    • 1970-01-01
    • 2021-03-13
    • 1970-01-01
    • 1970-01-01
    • 2020-04-27
    相关资源
    最近更新 更多