'Parameterize fixture factory in Pytest

I have a dummy name factory fixture:

@pytest.fixture
def dummy_name():
    def func(name="Dummy Name"):
        yield name
        num = 2
        while True:
            yield f"{name}_{num}"
            num += 1

    return func

And I have a fixture which utilizes this:

@pytest.fixture
def dummy_document_file_name(dummy_name, extension):
    if extension:
        return dummy_name(name="test_file" + f".{extension}")
    return dummy_name(name="test_file.dummy_extension")

However extension supposed to be not another fixture but a parameter provided by the user of this fixture.

So I'd like to achieve this:

dummy_file = next(dummy_document_file_name(extension="txt")) # test_file.txt
dummy_file2 = next(dummy_document_file_name(extension="txt")) # test_file2.txt

Naturally it looks for another fixture:

  @pytest.fixture
  def dummy_document_file_name(dummy_name, extension):
E       fixture 'extension' not found
>       available fixtures: ...

Is there a way to inject non-fixture parameters?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source