Skip to content

Reset

provide.testkit.logger.reset

Foundation Reset Utilities for Testing.

Thin wrapper around Foundation's orchestrated reset functionality. Provides backward-compatible API while delegating to Foundation's internal reset orchestration.

Testing Patterns with Dependency Injection

With Foundation's dependency injection architecture, there are two main testing patterns:

  1. Global State Testing (traditional): Uses the global Hub via get_hub() and requires reset functions between tests. Suitable for integration tests and legacy code.

  2. Isolated Container Testing (DI-based): Creates isolated Container instances per test, eliminating the need for global state resets. Each test gets a fresh Container and Hub automatically.

Example: >>> from provide.foundation.hub import Hub, Container >>> def test_my_feature(): ... container = Container() ... hub = Hub(container=container) ... # Test has clean, isolated state

The reset functions in this module support the traditional global state pattern. Tests using isolated containers can skip these reset calls entirely.

Functions

mock_logger

mock_logger() -> Mock

Comprehensive mock logger for testing.

Provides compatibility with both stdlib logging and structlog interfaces, including method call tracking and common logger attributes.

Returns:

Type Description
Mock

Mock logger with debug, info, warning, error methods and structlog compatibility.

Source code in provide/testkit/logger/mocks.py
@pytest.fixture
def mock_logger() -> Mock:
    """
    Comprehensive mock logger for testing.

    Provides compatibility with both stdlib logging and structlog interfaces,
    including method call tracking and common logger attributes.

    Returns:
        Mock logger with debug, info, warning, error methods and structlog compatibility.
    """
    logger = Mock()
    logger.debug = Mock()
    logger.info = Mock()
    logger.warning = Mock()
    logger.warn = Mock()  # Alias for warning
    logger.error = Mock()
    logger.exception = Mock()
    logger.critical = Mock()
    logger.fatal = Mock()  # Alias for critical

    # Add common logger attributes
    logger.name = "mock_logger"
    logger.level = 10  # DEBUG level
    logger.handlers = []
    logger.disabled = False

    # Add structlog compatibility methods
    logger.bind = Mock(return_value=logger)
    logger.unbind = Mock(return_value=logger)
    logger.new = Mock(return_value=logger)
    logger.msg = Mock()  # Alternative to info

    # Add trace method for Foundation's extended logging
    logger.trace = Mock()

    return logger

mock_logger_factory

mock_logger_factory() -> Mock

Factory function to create mock loggers outside of pytest context.

Useful for unit tests that need a mock logger but aren't using pytest fixtures.

Returns:

Type Description
Mock

Mock logger with the same interface as the pytest fixture.

Source code in provide/testkit/logger/mocks.py
def mock_logger_factory() -> Mock:
    """
    Factory function to create mock loggers outside of pytest context.

    Useful for unit tests that need a mock logger but aren't using pytest fixtures.

    Returns:
        Mock logger with the same interface as the pytest fixture.
    """
    logger = Mock()
    logger.debug = Mock()
    logger.info = Mock()
    logger.warning = Mock()
    logger.warn = Mock()
    logger.error = Mock()
    logger.exception = Mock()
    logger.critical = Mock()
    logger.fatal = Mock()

    logger.name = "mock_logger"
    logger.level = 10
    logger.handlers = []
    logger.disabled = False

    logger.bind = Mock(return_value=logger)
    logger.unbind = Mock(return_value=logger)
    logger.new = Mock(return_value=logger)
    logger.msg = Mock()
    logger.trace = Mock()

    return logger

reset_foundation_setup_for_testing

reset_foundation_setup_for_testing() -> None

Complete Foundation reset for testing with all test-specific concerns.

This function ensures clean test isolation by resetting all Foundation state between test runs using Foundation's own orchestrated reset functionality.

Note on Dependency Injection

With Foundation's new dependency injection architecture, tests that use isolated Container instances may not need to call this global reset function. If your test creates its own Hub with an isolated Container, the test inherently has clean state without resetting global state.

Example of isolated testing with DI: >>> from provide.foundation.hub import Hub, Container >>> def test_with_isolated_container(): ... # No reset needed - each test gets fresh container ... container = Container() ... hub = Hub(container=container) ... # Use hub for dependency injection ... # Test has isolated state automatically

Continue using this function for: - Tests that rely on global Hub state via get_hub() - Integration tests that need to reset shared state - Legacy tests not yet using explicit dependency injection

Source code in provide/testkit/logger/reset.py
def reset_foundation_setup_for_testing() -> None:
    """Complete Foundation reset for testing with all test-specific concerns.

    This function ensures clean test isolation by resetting all
    Foundation state between test runs using Foundation's own
    orchestrated reset functionality.

    Note on Dependency Injection:
        With Foundation's new dependency injection architecture, tests that use
        isolated Container instances may not need to call this global reset
        function. If your test creates its own Hub with an isolated Container,
        the test inherently has clean state without resetting global state.

        Example of isolated testing with DI:
            >>> from provide.foundation.hub import Hub, Container
            >>> def test_with_isolated_container():
            ...     # No reset needed - each test gets fresh container
            ...     container = Container()
            ...     hub = Hub(container=container)
            ...     # Use hub for dependency injection
            ...     # Test has isolated state automatically

        Continue using this function for:
            - Tests that rely on global Hub state via get_hub()
            - Integration tests that need to reset shared state
            - Legacy tests not yet using explicit dependency injection
    """
    from provide.foundation.testmode.orchestration import reset_foundation_for_testing

    reset_foundation_for_testing()

reset_foundation_state

reset_foundation_state() -> None

Reset Foundation's complete internal state using Foundation's orchestration.

This is a thin wrapper around Foundation's internal reset orchestration. Use reset_foundation_setup_for_testing() for the full test reset.

Note on Dependency Injection

Tests using isolated Container instances for dependency injection may not need to reset global state. See reset_foundation_setup_for_testing() documentation for details on testing with isolated containers.

Source code in provide/testkit/logger/reset.py
def reset_foundation_state() -> None:
    """Reset Foundation's complete internal state using Foundation's orchestration.

    This is a thin wrapper around Foundation's internal reset orchestration.
    Use reset_foundation_setup_for_testing() for the full test reset.

    Note on Dependency Injection:
        Tests using isolated Container instances for dependency injection may not
        need to reset global state. See reset_foundation_setup_for_testing()
        documentation for details on testing with isolated containers.
    """
    from provide.foundation.testmode.orchestration import reset_foundation_state as foundation_reset

    foundation_reset()