Skip to content

service_locator

Scope: function | Returns: DependencyContainer

The service_locator fixture creates and sets a generic service locator bootstrapped with BootstrapperForTests. It is a fallback for tests that depend on the service locator but do not fit any specific service context.

Last resort

This fixture should not be used in most cases. If you are testing code that runs in the context of a specific service — webserver, task handler, event handler, task scheduler, limefu, system jobs, or search indexer — use the dedicated fixture for that service instead. Using service_locator in those contexts means the dependency injection container is not bootstrapped with the correct service components, and the test may pass against an incorrect setup.

Dedicated service locator fixtures

Each service has its own fixture that bootstraps the container with the correct component registrations loaded from that service's entrypoints:

Fixture Use when testing
task_handler_service_locator Background tasks
task_scheduler_service_locator Task scheduling
event_handler_service_locator Event handlers
webserver_service_locator HTTP endpoints
limefu_service_locator limefu commands
system_jobs_service_locator System jobs
search_indexer_service_locator Search indexer

When service_locator is appropriate

Use service_locator only when the code under test depends on the service locator but does not belong to any of the service contexts listed above — for example, a utility or infrastructure component that resolves dependencies at runtime and cannot be tested without a container being set.

from lime_config import ApplicationConfigRepository

def test_component_can_be_resolved(service_locator):
    repository = service_locator.get(service=ApplicationConfigRepository)

    assert repository is not None