Commit c2177195 authored by Samuel GAIST's avatar Samuel GAIST Committed by Samuel Gaist
Browse files

[test][utils] Add mock logging handler

This will allow to analyse the content of logs generated during
a test.
parent 66f2507b
......@@ -29,6 +29,7 @@ Helper functions to run tests
"""
import os
import logging
from beat.core.experiment import Experiment
from beat.core.hash import toPath
......@@ -53,3 +54,35 @@ def index_experiment_dbs(experiment_name):
infos['set']),
suffix='.db')
view.index(os.path.join(tmp_prefix, filename))
# Based on https://stackoverflow.com/a/20553331/5843716
class MockLoggingHandler(logging.Handler):
"""Mock logging handler to check for expected logs.
Messages are available from an instance's ``messages`` dict, in order,
indexed by a lowercase log level string (e.g., 'debug', 'info', etc.).
"""
def __init__(self, *args, **kwargs):
self.messages = {
'debug': [], 'info': [],
'warning': [], 'error': [],
'critical': [], 'extra': []
}
super(MockLoggingHandler, self).__init__(*args, **kwargs)
def emit(self, record):
"Store a message from ``record`` in the instance's ``messages`` dict."
try:
self.messages[record.levelname.lower()].append(record.getMessage())
except Exception:
self.handleError(record)
def reset(self):
self.acquire()
try:
for message_list in self.messages.values():
message_list.clear()
finally:
self.release()
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment