Skip to content

ensure_service_locator

Scope: function | Returns: None

The ensure_service_locator fixture guarantees that a service locator is bootstrapped and set before the test runs. Rather than imposing a specific one, it defers to whichever service locator fixture is already active in the test context. If none is found, it falls back to service_locator.

This makes it useful for writing fixtures that need the service locator to be ready but should not dictate which service context the test runs in.

Warning

A RuntimeError is raised if more than one service locator fixture is declared in the same test.

Basic usage

Declare it as a dependency in a fixture that requires the service locator to be initialised, without coupling it to a specific service:

@pytest.fixture
def my_fixture(ensure_service_locator):
    # service locator is guaranteed to be set here
    ...