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.
pip install pytest-loguruNo compatibility data collected yet for this library.
This quickstart demonstrates how to use the `caplog` fixture to capture messages emitted by Loguru. After installing `pytest-loguru`, the `caplog` fixture automatically intercepts Loguru messages, making them available for assertions via `caplog.text` and `caplog.records`.
Ensure your tests are designed for Loguru's logging mechanisms when `pytest-loguru` is active. For `logging` module specific tests, consider running them in an environment without `pytest-loguru` or using Loguru's configuration options to achieve similar effects.
To prevent duplicate output, you can use `logger.remove()` and `logger.add()` within your test setup (e.g., a fixture) to control Loguru's handlers, ensuring only the necessary ones (which `pytest-loguru` can intercept) are active during the test.
Configure Loguru using its native API (e.g., `logger.remove()`, `logger.add(sys.stderr, level='WARNING')`) to control logging levels and destinations within your tests.
Verify `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.
Confirm `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.
Use 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.