Pytest Loguru
pytest-loguru is a pytest plugin that integrates Loguru with pytest's `caplog` fixture. It replaces the default `caplog` fixture to seamlessly capture log messages emitted by Loguru, allowing for easy assertion in tests. The current version is 0.4.0, and it maintains a moderate release cadence with updates addressing compatibility and minor improvements.
Common errors
-
AssertionError: 'Your Loguru message' not in caplog.text
cause Loguru messages are not being captured by `caplog`. This usually happens if `pytest-loguru` is not installed, not enabled, or Loguru's handlers are configured in a way that prevents interception.fixVerify `pytest-loguru` is installed (`pip install pytest-loguru`). Ensure Loguru has at least one handler configured (e.g., the default `sys.stderr` handler) that the plugin can intercept. Avoid `logger.remove()` without a subsequent `logger.add()` that directs output to a stream `pytest-loguru` monitors. -
Log messages appear in the console/stderr during tests, but `caplog.text` is empty.
cause Similar to the above, the `pytest-loguru` plugin might not be active, or Loguru is configured to send logs to handlers that `pytest-loguru` does not monitor.fixConfirm `pytest-loguru` is correctly installed. The plugin primarily intercepts messages sent to `sys.stderr`. If you've redirected Loguru's output to files or custom handlers, `caplog` may not see them. Ensure a handler writing to `sys.stderr` is active or configure Loguru to log to a stream that can be captured. -
TypeError: 'LoguruLogger' object has no attribute 'setLevel'
cause This error occurs when trying to use `logging` module methods (like `setLevel`) directly on a `loguru.logger` instance. Loguru's API for configuration is distinct from the standard `logging` module.fixUse Loguru's own methods for configuration. For example, to set the level, use `logger.remove()` and `logger.add(sys.stderr, level='INFO')` or configure handlers explicitly.
Warnings
- gotcha The `pytest-loguru` plugin replaces pytest's default `caplog` fixture. If you have tests that rely on specific behaviors of the standard `caplog` for `logging` module messages, or use `caplog.set_level()`, you might encounter unexpected behavior as `pytest-loguru` intercepts Loguru's output instead.
- gotcha Loguru, by default, outputs to `sys.stderr`. While `pytest-loguru` captures this for `caplog`, you might still see duplicate output on the console during test runs if Loguru's handlers are not managed correctly within your tests or if multiple handlers are configured for the logger.
- gotcha Attempting to use standard `logging` module methods like `caplog.set_level()` or `logger.propagate = False` directly on the `loguru.logger` object will not work, as Loguru's API is different from the standard `logging` module.
Install
-
pip install pytest-loguru
Quickstart
from loguru import logger
import pytest
def test_loguru_messages_captured(caplog):
logger.info("This is an info message from Loguru")
logger.warning("This is a warning message from Loguru")
assert "This is an info message from Loguru" in caplog.text
assert "This is a warning message from Loguru" in caplog.text
assert caplog.records[0].levelname == "INFO"
assert caplog.records[1].levelname == "WARNING"