I understand that pytest fixtures raise an error when calling a fixture directly from within a test. But I don't fully understand why. For context, I am a junior dev new to python so I may be missing something obvious that needs explaining.
I have a fixture as follows:
@pytest.fixture()
def get_fixture_data_for(sub_directory: str, file_name: str) -> json:
file_path = Path(__file__).parent / f"{sub_directory}"
with open(file_path / f"{file_name" as j:
data = json.load(j)
return data
and then a test that says something like
def test_file_is_valid():
data = get_fixture_data_for('some_subdir', 'some_filename.json')
#do something with this information
...
I have many different test files that will use this fixture function to read data from the files and then use the data in posting to an endpoint for integration tests.
When running this, I get the error that fixtures are not meant to be called directly, and should be created automatically when test functions request them as parameters
. But why?
I see in the docs this is mentioned: https://docs.pytest.org/en/stable/deprecations.html#calling-fixtures-directly but I don't understand why this functionality was deprecated.