0

Is there possibility to parametrize fixture from a fixture?

Let's say I've a fixture, which is taking relay_number as a parameter:

@pytest.fixture
def unipi_relay(request):
    try:
        relay_number = request.param["relay_number"]
    except KeyError:
        raise ValueError(
            "This function requires as a parameter dictionary with values for keys:"
            "\nrelay_number - passed as integer\n"
        )

    relay = RelayFactory.get_unipi_relay(relay_number)
    relay.reset()
    yield relay
    relay.reset()

Now I would like to have another fixture, which will yield unipi_relay with already passed parameter.

Reason why I want to implement such a solution is that I would like to reuse unipi_relay fixture a few times in single test.

Mateusz
  • 219
  • 2
  • 13
  • Maybe this is what you're looking for: [Pass a parameter to a fixture function](https://stackoverflow.com/questions/18011902/pass-a-parameter-to-a-fixture-function) – Marco.S Mar 30 '21 at 16:51
  • Thanks, but unfortunately not. This is about parametrizing the fixture from a test. What I'm looking for is parametrizing the fixture from a fixture. – Mateusz Mar 31 '21 at 17:18

1 Answers1

0

I'm not sure if I'm understanding correctly what you want to achieve because you haven't put the parameters your fixture is taking. Maybe the “factory as fixture” pattern is what you're looking for, because you'll then be able to reuse the unipi_relay fixture. Please also have a look at the question Reusing pytest fixture in the same test.

Marco.S
  • 383
  • 2
  • 10