Logger Testing¶
provide.testkit.logger
¶
Logger Testing Utilities.
Provides utilities for logger testing, including state reset, mock fixtures, and pytest hooks for managing noisy loggers.
Functions¶
get_log_level_for_noisy_loggers
¶
Get the log level to set for noisy loggers.
Can be customized via the TESTKIT_NOISY_LOG_LEVEL environment variable. Defaults to WARNING level.
Returns:
| Type | Description |
|---|---|
int
|
Logging level (int) to set for noisy loggers. |
Source code in provide/testkit/logger/hooks.py
get_noisy_loggers
¶
Get the list of loggers to suppress during tests.
Can be customized via the TESTKIT_NOISY_LOGGERS environment variable, which should be a comma-separated list of logger names.
Returns:
| Type | Description |
|---|---|
list[str]
|
List of logger names to suppress. |
Source code in provide/testkit/logger/hooks.py
pytest_runtest_setup
¶
Hook that runs before each test setup.
This forcefully sets the log level for noisy libraries to WARNING (or custom level), overriding any configuration that may have happened at import time (e.g., by Textual or the application itself).
Source code in provide/testkit/logger/hooks.py
suppress_loggers
¶
Utility function to suppress specific loggers to a given level.
Can be used directly in tests or conftest.py files for custom suppression.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
logger_names
|
list[str]
|
List of logger names to suppress |
required |
level
|
int
|
Log level to set (defaults to WARNING) |
WARNING
|
Source code in provide/testkit/logger/hooks.py
mock_logger
¶
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
mock_logger_factory
¶
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
reset_foundation_setup_for_testing
¶
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
reset_foundation_state
¶
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.