简体   繁体   中英

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 (eg)

from .fixtures.models import Account

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

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.)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM