Fixtures¶
lime-test is a pytest plugin, so its fixtures are available in every test without any imports. The pages in this section describe the most commonly used ones. Run pytest --fixtures to list everything that is available — fixtures not described here can be used at your own risk.
Categories¶
| Category | Use for |
|---|---|
| Schema | Defining the limetypes your tests run against |
| Database | The test database and connections to it |
| Application | LimeApplication instances and their composition |
| Users & Groups | Creating users and groups |
| Data | Saving lime objects and files |
| Configuration | Settings, service and application configuration |
| HTTP | Test clients for endpoint tests |
| Tasks | Mocking background task dispatch and status |
| Service Locator | Bootstrapping dependency injection per service context |
Conventions¶
- Each test runs against a fresh in-memory database — see
database— so state never leaks between tests. - Factory fixtures (
create_user,create_group, ...) return a callable that the test invokes, possibly multiple times. - The creation factories for users and groups return the created record's ID by default. Pass
_return_entity=Trueto get the full object instead — returning the ID is faster, so only request the entity when you need it.
Replacing a fixture¶
As per standard pytest behavior, any fixture can be replaced by defining a fixture with the same name closer to your tests, typically in a conftest.py. However, consider doing that in a specific module if the fixture is relevant there only. The replacement must keep the original's scope and return type, so it composes with the rest of the fixtures:
import pytest
import lime_type
@pytest.fixture(scope="session")
def lime_types():
return lime_type.create_limetypes_from_dsl({
"company": {
"name": {"type": "string", "required": True, "length": 200},
},
})
Extending a fixture¶
To build on top of a fixture instead of replacing it, define a fixture with the same name that takes the original as a parameter. pytest resolves the parameter to the definition from lime-test:
import pytest
@pytest.fixture
def lime_app(lime_app, save_lime_objects):
coworker = lime_app.limetypes.coworker(firstname="Admin", lastname="User")
save_lime_objects(coworker, application=lime_app)
return lime_app
The scope and return type must match the original here as well.