import io import json import logging import logging.handlers import unittest from unittest.mock import patch from src.icaotix_python_logging import logger class TestBasicLogger(unittest.TestCase): def test_creation(self): log = logger.get_stdout_logger("test_basic_logger") self.assertIsInstance(log, logging.Logger) self.assertEqual(1, len(log.handlers)) self.assertIsInstance(log.handlers[0], logging.handlers.QueueHandler) @patch('sys.stdout', new_callable=io.StringIO) def test_single_log_message(self, stdout: io.StringIO): log = logger.get_stdout_logger("test_basic_logger") log.info("Hello world") # wait for all writes to be done (type is checked in test above) log.handlers[0].listener.queue.join() actual_json = json.loads(stdout.getvalue()) self.assertEqual(actual_json, actual_json | {"logger": "test_basic_logger", "message": "Hello world"}) if __name__ == '__main__': unittest.main()