[Python] Can I parameterize a pytest fixture with other fixtures?

Discussão em 'Python' iniciado por Stack, Dezembro 6, 2024.

  1. Stack

    Stack Membro Participativo

    I have a python test that uses a fixture for credentials (a tuple of userid and password)

    def test_something(credentials)
    (userid, password) = credentials
    print("Hello {0}, welcome to my test".format(userid))


    and I have pytest fixture for credentials:

    @pytest.fixture()
    def credentials():
    return ("my_userid", "my_password")


    It works great

    Now I want to extend this for multiple credentials (say staging and production) so that my test will run twice (once each for staging and production).

    I thought that parameterization was the answer, but it seems I can't parameterize with fixtures.

    I would love to do something like this:

    @pytest.fixture(params=[staging_credentials, production_credentials])
    def credentials(request):
    return request.param


    where staging_credentials and production_credentials are all fixtures:

    @pytest.fixture()
    def staging_credentials():
    return ("staging_userid", "staging_password")

    @pytest.fixture()
    def production_credentials():
    return ("prod_userid", "prod_password")


    but apparently parameters to the fixture can't be other fixtures.

    Any suggestions on how to elegantly handle this? I've looked at https://docs.pytest.org/en/latest/proposals/parametrize_with_fixtures.html but that seems a but too brute force.

    Thanks! Steve

    Continue reading...

Compartilhe esta Página