6

I have been testing my custom loggers with Pytest. Created a custom logger from Yaml config file, and wrote the following test:

@pytest.fixture(scope="module")
def logger():
    """Fixture for providing a configured logger object"""
    logging_config = yaml.safe_load(config_yaml)
    logging.config.dictConfig(logging_config)
    return logging.getLogger("test_logger")


def test_logger_emits_with_queue_handler(logger, caplog):
    """Test fails if queue handler could not emit logs"""
    logger.info("This is a test")
    assert "This is a test" in caplog.text

This works and the test passes as expected.

However, when I try to do with without fixtures, the test fails:

def test_logger_emits_with_queue_handler(caplog):
    """Test fails if queue handler could not emit logs"""
    logging_config = yaml.safe_load(config_yaml)
    logging.config.dictConfig(logging_config)
    logger = logging.getLogger("test_logger")
    logger.info("This is a test")
    assert "This is a test" in caplog.text

For reference, my configuration yaml file, custom queue handler is from my logging library:

version: 1
objects:
  queue:
    class: queue.Queue
    maxsize: 1000
formatters:
  simple:
    format: '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
handlers:
  console:
    class: logging.StreamHandler
    formatter: simple
    stream: ext://sys.stdout
  queue_handler:
    class: logging_.handlers.QueueListenerHandler
    handlers:
      - cfg://handlers.console
    queue: cfg://objects.queue
loggers:
  test_logger:
    level: DEBUG
    handlers:
      - queue_handler
    propagate: yes
root:
  level: NOTSET
  handlers:
    - console

I am trying to understand the reason for this. Is it because a standard logger object has already been configured by the time Pytest loads and runs the test method? Is there a workaround for doing this without writing a fixture?

Zobayer Hasan
  • 2,187
  • 6
  • 22
  • 38
  • Just curious. If you use a context manager and set level accordingly, does it make any difference? i.e. `with caplog.at_level(logging.DEBUG):` Not an educated guess, just know that works for me without a custom logger. – Evan Nov 09 '21 at 21:46
  • @Evan I believe I tried that, it did not work for this case. My (also not very educated) guess is that, the logger gets already initialized before pytest reach the function, and what I do in that function does not really matter anymore. – Zobayer Hasan Nov 10 '21 at 10:23
  • Did you finally find a way around? (facing similar issue) – zar3bski Jul 06 '22 at 12:04
  • No, haven't gotten any workaround. – Zobayer Hasan Sep 13 '22 at 10:01

0 Answers0