Pytest fixtures 不应该被直接调用

Pytest fixtures are not meant to be called directly

我想在 conftest.py 中设置一个夹具并与我的所有测试共享。但是,当 运行 在以下最小示例中使用 pytest 时,我 运行 出现以下错误:

ERROR at setup of test_test 
Fixture "pytest_runtest_setup" called directly. Fixtures are not meant to be called directly,
but are created automatically when test functions request them as parameters.

conftest.py

import pytest
import os


@pytest.fixture
def pytest_runtest_setup():
    print("hello ? ---------------------------------")

test.py

import pytest


def test_test():
    assert True

pytest.ini

[pytest]
addopts = -rA -v -p no:faulthandler -W ignore::DeprecationWarning

Pytest 随 pytest test.py

启动

没有直接调用fixture函数的地方,甚至没有在示例测试中使用。那么为什么 pytest 抛出这个以及我如何声明和使用固定装置到 conftest.py?

环境:

夹具的名字是pytest_runtest_setup,实际上是一个你覆盖的hook,正在

Called to perform the setup phase for a test item.

所以直接调用

Pytest 6 不支持 calling fixtures directly

Removed in version 4.0.

Calling a fixture function directly, as opposed to request them in a test function, is deprecated.

将夹具重命名为 runtest_setup(或其他名称)应该可以解决问题。