Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Find a cleaner pattern for fixtures with arguments #395

Closed
simonw opened this issue Jan 6, 2019 · 1 comment
Closed

Find a cleaner pattern for fixtures with arguments #395

simonw opened this issue Jan 6, 2019 · 1 comment

Comments

@simonw
Copy link
Owner

simonw commented Jan 6, 2019

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")
@simonw
Copy link
Owner Author

simonw commented Jun 7, 2020

This is a pattern I like:

    with make_app_client(
        template_dir=str(pathlib.Path(__file__).parent / "test_templates")
    ) as client:
        response = client.get("/-/metadata")
        assert response.status == 200

@simonw simonw closed this as completed in abc7339 Jun 7, 2020
simonw added a commit that referenced this issue Jun 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant