Itération 122442
Itération 122442

Reputation: 2972

Pytest fixtures are not meant to be called directly

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:

Upvotes: 5

Views: 8313

Answers (1)

Guy
Guy

Reputation: 50949

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.

Rename the fixture to runtest_setup or anything else as long as the fixture name doesn't start with pytest_ when in conftest.py or it will be recognized as a hook (there is no issue with the name when located in the test file).

Upvotes: 6

Related Questions