Skip to content

Commit

Permalink
Clear caplog and use as context manager in test_logging (#3382)
Browse files Browse the repository at this point in the history
  • Loading branch information
snarayan21 committed Jun 7, 2024
1 parent 12b2e82 commit ffbf163
Showing 1 changed file with 19 additions and 18 deletions.
37 changes: 19 additions & 18 deletions tests/test_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,24 +306,25 @@ def test_logging(
monkeypatch: pytest.MonkeyPatch,
):
"""Test that engine logs statements as expected"""
caplog.set_level(logging.DEBUG, logger=Engine.__module__)
# Include a callback, since most logging happens around callback events
dummy_state.callbacks = [EventCounterCallback()]

monkeypatch.setenv('ENGINE_DEBUG', '1')
engine = Engine(dummy_state, dummy_logger)
engine.run_event('INIT')
engine.close()

# Validate that we have the expected log entries
assert caplog.record_tuples == [
('composer.core.engine', 10, '[ep=0][ba=0][event=INIT]: Running event'),
('composer.core.engine', 10, '[ep=0][ba=0][event=INIT]: Running callback EventCounterCallback'),
('composer.core.engine', 10, 'Closing the engine.'),
('composer.core.engine', 10, 'Closing callback EventCounterCallback'),
('composer.core.engine', 10, 'Post-closing callback EventCounterCallback'),
('composer.core.engine', 10, 'Engine closed.'),
]
caplog.clear()
with caplog.at_level(logging.DEBUG, logger=Engine.__module__):
# Include a callback, since most logging happens around callback events
dummy_state.callbacks = [EventCounterCallback()]

monkeypatch.setenv('ENGINE_DEBUG', '1')
engine = Engine(dummy_state, dummy_logger)
engine.run_event('INIT')
engine.close()

# Validate that we have the expected log entries
assert caplog.record_tuples == [
('composer.core.engine', 10, '[ep=0][ba=0][event=INIT]: Running event'),
('composer.core.engine', 10, '[ep=0][ba=0][event=INIT]: Running callback EventCounterCallback'),
('composer.core.engine', 10, 'Closing the engine.'),
('composer.core.engine', 10, 'Closing callback EventCounterCallback'),
('composer.core.engine', 10, 'Post-closing callback EventCounterCallback'),
('composer.core.engine', 10, 'Engine closed.'),
]


def _worker():
Expand Down

0 comments on commit ffbf163

Please sign in to comment.