Skip to content

Commit

Permalink
add test for set_logger context manager
Browse files Browse the repository at this point in the history
  • Loading branch information
Gautzilla committed Oct 30, 2024
1 parent a28932f commit 4bf94f9
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions tests/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import logging
import importlib

from OSmOSE.LoggingContext import LoggingContext


@pytest.fixture
def temp_user_logging_config(tmp_path):
Expand Down Expand Up @@ -106,3 +108,28 @@ def test_default_logging_config(caplog, tmp_path):
logging.getLogger().debug("Some debug log")

assert "Some debug log" in caplog.text


@pytest.mark.unit
def test_logging_context(caplog):
logging_context = LoggingContext()

context_logger = logging.getLogger("context_logger")
logging_context.logger = logging.getLogger("default_logger")

with caplog.at_level(logging.DEBUG):
logging_context.logger.debug("From default logger")

with logging_context.set_logger(context_logger):
logging_context.logger.debug("From context logger")

logging_context.logger.debug("From default logger again")

assert len(caplog.records) == 3

assert caplog.records[0].name == "default_logger"
assert caplog.records[0].message == "From default logger"
assert caplog.records[1].name == "context_logger"
assert caplog.records[1].message == "From context logger"
assert caplog.records[2].name == "default_logger"
assert caplog.records[2].message == "From default logger again"

0 comments on commit 4bf94f9

Please sign in to comment.