Skip to content

Conftest

provide.testkit.conftest

Pytest configuration and fixtures for provide-testkit.

Functions

default_container_directory

default_container_directory() -> Any

Provides a default directory for container operations in tests.

This fixture is used by tests that need a temporary directory for container-related operations.

Source code in provide/testkit/hub/fixtures.py
@pytest.fixture(scope="session")
def default_container_directory() -> Any:
    """
    Provides a default directory for container operations in tests.

    This fixture is used by tests that need a temporary directory
    for container-related operations.
    """
    with foundation_temp_dir() as tmp_dir:
        yield tmp_dir

isolated_container

isolated_container() -> Any

Create an isolated Container for dependency injection testing.

This fixture provides a fresh Container instance for each test, eliminating the need for global state resets between tests.

Returns:

Name Type Description
Container Any

A new Container instance with no pre-registered dependencies.

Example

def test_my_component(isolated_container): ... # Register test-specific dependencies ... isolated_container.register("my_service", MyService()) ... # Test with isolated state ... service = isolated_container.resolve("my_service") ... assert service is not None

Note

Tests using this fixture do not need to call reset_foundation_setup_for_testing() as each test gets a fresh container automatically.

Source code in provide/testkit/hub/fixtures.py
@pytest.fixture
def isolated_container() -> Any:
    """
    Create an isolated Container for dependency injection testing.

    This fixture provides a fresh Container instance for each test,
    eliminating the need for global state resets between tests.

    Returns:
        Container: A new Container instance with no pre-registered dependencies.

    Example:
        >>> def test_my_component(isolated_container):
        ...     # Register test-specific dependencies
        ...     isolated_container.register("my_service", MyService())
        ...     # Test with isolated state
        ...     service = isolated_container.resolve("my_service")
        ...     assert service is not None

    Note:
        Tests using this fixture do not need to call
        reset_foundation_setup_for_testing() as each test
        gets a fresh container automatically.
    """
    from provide.foundation.hub import Container

    return Container()

isolated_hub

isolated_hub() -> Any

Create an isolated Hub with fresh registries for testing.

This fixture provides a Hub instance with isolated registries, enabling dependency injection testing without global state side effects.

Returns:

Name Type Description
Hub Any

A Hub instance with isolated registries.

Example

def test_with_di(isolated_hub): ... # Hub has fresh, isolated state ... client = UniversalClient(hub=isolated_hub) ... # Test proceeds without affecting global Hub ... response = await client.get("https://api.example.com") ... assert response.status == 200

Note

This fixture is ideal for unit tests that need Hub functionality but want complete isolation from other tests. No reset functions are needed when using this fixture.

Source code in provide/testkit/hub/fixtures.py
@pytest.fixture
def isolated_hub() -> Any:
    """
    Create an isolated Hub with fresh registries for testing.

    This fixture provides a Hub instance with isolated registries,
    enabling dependency injection testing without global state side effects.

    Returns:
        Hub: A Hub instance with isolated registries.

    Example:
        >>> def test_with_di(isolated_hub):
        ...     # Hub has fresh, isolated state
        ...     client = UniversalClient(hub=isolated_hub)
        ...     # Test proceeds without affecting global Hub
        ...     response = await client.get("https://api.example.com")
        ...     assert response.status == 200

    Note:
        This fixture is ideal for unit tests that need Hub functionality
        but want complete isolation from other tests. No reset functions
        are needed when using this fixture.
    """
    from provide.foundation.context import CLIContext
    from provide.foundation.hub import Hub
    from provide.foundation.hub.registry import Registry

    # Create isolated registries
    component_registry = Registry()
    command_registry = Registry()

    # Create Hub with isolated registries (not shared)
    return Hub(
        context=CLIContext(),
        component_registry=component_registry,
        command_registry=command_registry,
        use_shared_registries=False,
    )

pytest_sessionfinish

pytest_sessionfinish(
    session: Session, exitstatus: int
) -> None

Reset terminal state after test session to prevent output corruption.

This hook ensures ANSI escape codes and terminal state are properly reset after pytest completes, preventing garbled output in the terminal.

Common causes of terminal corruption: - pytest-benchmark unicode box-drawing characters - Hypothesis verbose output with special characters - ANSI color codes not properly terminated - pytest-xdist worker output interference

The reset codes used: - \033[0m : Reset all text formatting attributes - \033[?25h : Show cursor (in case it was hidden)

Source code in provide/testkit/conftest.py
def pytest_sessionfinish(session: pytest.Session, exitstatus: int) -> None:
    """
    Reset terminal state after test session to prevent output corruption.

    This hook ensures ANSI escape codes and terminal state are properly reset
    after pytest completes, preventing garbled output in the terminal.

    Common causes of terminal corruption:
    - pytest-benchmark unicode box-drawing characters
    - Hypothesis verbose output with special characters
    - ANSI color codes not properly terminated
    - pytest-xdist worker output interference

    The reset codes used:
    - \\033[0m  : Reset all text formatting attributes
    - \\033[?25h : Show cursor (in case it was hidden)
    """
    import sys

    # Reset terminal: clear all formatting and restore cursor
    reset_sequence = "\033[0m\033[?25h"
    sys.stdout.write(reset_sequence)
    sys.stdout.flush()
    sys.stderr.write(reset_sequence)
    sys.stderr.flush()