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

标签 python python-3.x 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 中声明和使用设备?
环境:
  • Python 3.8.3
  • pytest-6.1.2
  • 最佳答案

    fixture 名称是pytest_runtest_setup ,这实际上是一个 hook你覆盖的,并且正在

    Called to perform the setup phase for a test item.


    所以直接调用。
    Pytest 6 不支持 calling fixture s directly

    Removed in version 4.0.

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


    将 fixture 重命名为 runtest_setup (或其他任何东西)应该可以解决问题。

    关于python - Pytest 固定装置不应该被直接调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68160255/

    相关文章:

    python - 即使 `__init__()` 引发异常也使用对象?

    python - 异步单元测试 Sanic 应用程序会抛出 RuntimeError : this event loop is already running

    python - Pytest 找不到 Lambda 层中定义的函数

    python-3.x - 如果我在一个类中有多个测试并且前面的测试失败了,我如何让它跳过或退出该类而不是测试其余的测试?

    python - 如何将两个 Pandas DataFrame 与不同的、不重叠的 MultiIndex 结合起来?

    python - 未检索产品详细信息 json。只有当我们在 scraper api 中有商业和企业计划时,我们才能获取详细信息吗?

    python - Heroku/Django - 找不到 Postgresql 数据库

    python - 从嵌套字典创建列表

    python - CNN(Python Keras)的训练过程

    python - 按行值过滤 pandas 数据框时出现问题?