herr.kaste
herr.kaste

Reputation: 558

How to organize fixtures when using pytest

Fixtures tend to be small and reusable. Given that a specific fixture can rely on other fixtures

@pytest.fixture
def Account(db, memcache):
    ...

I would like to organize my fixtures in modules, and import them in a specific test-file like so (e.g.)

from .fixtures.models import Account

Unfortunately this does not seem to work. Instead I always have to import all subordinate fixtures too, e.g.

from .fixtures.models import Account, db, memcache

What is the better approach to have fine-grained small, reusable fixtures and make them accessible on a module level. (conftest works on the package/directory level.)

Upvotes: 5

Views: 2970

Answers (1)

hpk42
hpk42

Reputation: 23561

Usually i don't recommend this but if you have modules containing a specific set of fixtures (Which depend on each other), then maybe from .fixtures.models import * would be workable? I can't think of another generic solution at the moment which would avoid knowing the underlying fixture dependencies in the importing test module.

Upvotes: 1

Related Questions