From 7b04298eada91f3d8a523ac38db3ddd624d3e8bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mose=20M=C3=BCller?= Date: Mon, 16 Oct 2023 15:52:09 +0200 Subject: [PATCH] add logging tests --- tests/utils/test_logging.py | 71 +++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 tests/utils/test_logging.py diff --git a/tests/utils/test_logging.py b/tests/utils/test_logging.py new file mode 100644 index 0000000..bfde99b --- /dev/null +++ b/tests/utils/test_logging.py @@ -0,0 +1,71 @@ +import logging + +from pytest import LogCaptureFixture + +from pydase.utils.logging import setup_logging + + +def test_log_error(caplog: LogCaptureFixture): + setup_logging("ERROR") + logger = logging.getLogger() + logger.debug("This is a debug message") + logger.info("This is an info message") + logger.warning("This is a warning message") + logger.error("This is an error message") + + # Check the log records as well as the level. + assert "This is a debug message" not in caplog.text + assert "This is an info message" not in caplog.text + assert "This is a warning message" not in caplog.text + assert "This is an error message" in caplog.text + assert any(record.levelname == "ERROR" for record in caplog.records) + + +def test_log_warning(caplog: LogCaptureFixture): + setup_logging("WARNING") + logger = logging.getLogger() + logger.debug("This is a debug message") + logger.info("This is an info message") + logger.warning("This is a warning message") + logger.error("This is an error message") + + # Check the log records as well as the level. + assert "This is a debug message" not in caplog.text + assert "This is an info message" not in caplog.text + assert "This is a warning message" in caplog.text + assert "This is an error message" in caplog.text + assert any(record.levelname == "ERROR" for record in caplog.records) + + +def test_log_debug(caplog: LogCaptureFixture): + setup_logging("DEBUG") + logger = ( + logging.getLogger() + ) # Get the root logger or replace with the appropriate logger. + logger.debug("This is a debug message") + logger.info("This is an info message") + logger.warning("This is a warning message") + logger.error("This is an error message") + + # Now, check that the message is in the log records. + assert "This is a debug message" in caplog.text + assert "This is an info message" in caplog.text + assert "This is a warning message" in caplog.text + assert "This is an error message" in caplog.text + + +def test_log_info(caplog: LogCaptureFixture): + setup_logging("INFO") + logger = ( + logging.getLogger() + ) # Get the root logger or replace with the appropriate logger. + logger.debug("This is a debug message") + logger.info("This is an info message") + logger.warning("This is a warning message") + logger.error("This is an error message") + + # Now, check that the message is in the log records. + assert "This is a debug message" not in caplog.text + assert "This is an info message" in caplog.text + assert "This is a warning message" in caplog.text + assert "This is an error message" in caplog.text