How to test loguru logger with unittest?
See original GitHub issueI’ve got a project which is not using pytest. How can I successfully assert log statements made by loguru using unittests?
For example, I’ve got:
# test_loguru.py
import contextlib
import io
from unittest import TestCase
from loguru import logger
class LoggerTest(TestCase):
def test_logger_stderr(self):
f = io.StringIO()
with contextlib.redirect_stderr(f):
logger.info("loguru log right here")
self.assertEqual("loguru log right here\n", f.getvalue())
Running the test reveals I’m unsuccessful in capturing the loguru output:
$ python -m unittest test_loguru.py
2022-03-14 15:01:40.597 | INFO | test_loguru:test_logger_stderr:12 - loguru log right here
F
======================================================================
FAIL: test_logger_stderr (test_loguru.LoggerTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/fredrik/code/repos/logger/test_loguru.py", line 14, in test_logger_stderr
self.assertEqual("loguru log right here\n", f.getvalue())
AssertionError: 'loguru log right here\n' != ''
- loguru log right here
----------------------------------------------------------------------
Ran 1 test in 0.001s
FAILED (failures=1)
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
PyDev unittesting: How to capture text logged to a logging ...
Does anybody know how to capture everything that is logged to a logging.Logger during the execution of this test? python · unit-testing ·...
Read more >How To Unit Test Logging In Python (Asserting Logs) - YouTube
In this tutorial we will explore how to write unit test to check and assert you logs in python using unittest and pytest...
Read more >Switching from standard logging to loguru
Using Loguru, there is no need to explicitly get and name a logger, ... The caplog fixture captures logging output so that it...
Read more >How To Test Logging In Python?
1. Use The AssertLogs Context Manager From unitest ... If you are using unittest - the default Python testing library - you can...
Read more >loguru Documentation - Read the Docs
The rotation check is made before logging each message. ... method defined in the unittest from standard library is used to capture and...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Good point, I had forgotten that the format had to be adjusted as well!
I added the snippet to the documentation: Replacing
assertLogs()
method fromunittest
library.Thanks for bringing this to my attention. 👍
Very nice, this is a lot cleaner! 😃
I’m also thinking I’ll add in an arguments
level
andformat
(with some sane default), so I can tell individual tests what I’m actually probing for. Most of the time I don’t want to bother about the datetime string for example.Would be great to have this in the docs, yes! 😄