Skip to content

Fixture parametrization with a generator #4002

Closed
@Kkevsterrr

Description

@Kkevsterrr

I would like to be able to use a fixture parametrized with a generator.

For example:

def get_dbs():
     """
     Depending on how many tests are running concurrently, not every database
     will be available. Yield a database only if it's available.
     """
     # Rough sketch of the code: 
     while DB_LIST:
         for db in DB_LIST:
             if db.available():
                 # remove db from list, etc
                 yield db
@pytest.fixture(params=get_dbs())
def db(request):
    mydb = request.params
    ... # do stuff

However, it seems that when the params are given a generator, the generator is immediately casted to a list, and the generator is not used. The power of using a generator over a list is it gives you dynamic control of the fixture setup, like how you can do with tests already with parametrize.

You could, of course, use params as a placeholder to retrieve a database, and then dynamically retrieve an available database inside the fixture, but then the fixture name/id is lost to just "db0", instead of the actual useful name of "aurora_database5" (for example).

Am I missing something, or another way to accomplish this? Thanks for your help.

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: parametrizerelated to @pytest.mark.parametrizetype: questiongeneral question, might be closed after 2 weeks of inactivity

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions