Pytest caplog works with custom logger when created with a fixture, but not when created directly in test method

1.5k views Asked by At

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?

0

There are 0 answers