Pytest 固定装置不应该被直接调用

弗洛里安·卡斯泰兰

我想在 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 中声明和使用设备?

环境:

  • 蟒蛇 3.8.3
  • pytest-6.1.2
盖伊

夹具的名称是pytest_runtest_setup,它实际上是一个您覆盖钩子,并且正在

调用以执行测试项的设置阶段。

因此,它直接调用。

Pytest 6 不支持直接调用fixtures

在 4.0 版中删除。

不推荐直接调用夹具函数,而不是在测试函数中请求它们。

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章