Pytest fixtures are not meant to be called directly

Viewed 2808

I would like to set up a fixture in conftest.py and share it with all my tests. However, I run into the following error when running pytest on the following minimal example:

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 launched with pytest test.py

Nowhere is the fixture function called directly, it is not even used in the sample test. So why is pytest throwing this and how can I declare and use fixtures into conftest.py?

Env:

  • Python 3.8.3
  • pytest-6.1.2
1 Answers

The name of the fixture is pytest_runtest_setup, which is actually a hook that you override, and is being

Called to perform the setup phase for a test item.

So it is called directly.

Pytest 6 doesn't support calling fixtures directly

Removed in version 4.0.

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

Renaming the fixture to runtest_setup (or anything else) should resolve the issue.

Related