Skip to content

Find a cleaner pattern for fixtures with arguments #395

@simonw

Description

@simonw

A lot of Datasette tests look like this:

datasette/tests/test_api.py

Lines 438 to 444 in b65d977

def test_allow_sql_off():
for client in make_app_client(config={
'allow_sql': False,
}):
assert 400 == client.get(
"/fixtures.json?sql=select+sleep(0.01)"
).status

The loop here isn't actually expected to loop - it's there because the make_app_client function yields a value and then cleans it up afterwards.

This pattern works, but it is a little confusing. It would be nice to replace it with something less strange looking.

The answer may be to switch to the "factories as fixtures" pattern described here: https://docs.pytest.org/en/latest/fixture.html#factories-as-fixtures

In particular some variant of this example:

@pytest.fixture
def make_customer_record():

    created_records = []

    def _make_customer_record(name):
        record = models.Customer(name=name, orders=[])
        created_records.append(record)
        return record

    yield _make_customer_record

    for record in created_records:
        record.destroy()


def test_customer_records(make_customer_record):
    customer_1 = make_customer_record("Lisa")
    customer_2 = make_customer_record("Mike")
    customer_3 = make_customer_record("Meredith")

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions