Registry / testing / pytest-loguru

pytest-loguru

JSON →
library0.4.0pypypiunverified

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-loguru
INSTALL
IMPORT
SIG · PYTEST-LOGURU
P
pytest-loguru
testingpythonv0.4.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

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`.

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"
Debug
Known issues
gotchaThe `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.
fix
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.
affects: All versions
gotchaLoguru, 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.
fix
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.
affects: All versions
gotchaAttempting 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.
fix
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.
affects: All versions
Errors
Common errors & fixes
AssertionError: 'Your Loguru message' not in caplog.text
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.
fix
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.
Log messages appear in the console/stderr during tests, but `caplog.text` is empty.
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.
fix
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.
TypeError: 'LoguruLogger' object has no attribute 'setLevel'
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.
fix
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.
Upgrade
Version history
0.4.0latest on PyPI · released Mar 20, 2024
Audit
Dependencies
pytestrequiredThis is a pytest plugin and requires pytest to function.
logururequiredThis plugin integrates with Loguru and requires it to be installed to capture Loguru messages.
Agent activity
3 hits · last 30 days
node
2
OpenAI (training)
1
Resources
pytest-loguru — pip install pytest-loguru · libregistry