Added assertContains method to MockLoggingHandler
@@ -27,6 +27,8 @@ class MockLoggingHandler(logging.Handler):
def __init__(self, *args, **kwargs):
self.messages = {'DEBUG': [], 'INFO': [], 'WARNING': [],
'ERROR': [], 'CRITICAL': [], 'VERBOSE':[]}
+ self.param_methods = {'WARNING':'param.warning()', 'INFO':'param.message()',
+ 'VERBOSE':'param.verbose()', 'DEBUG':'param.debug()'}
super(MockLoggingHandler, self).__init__(*args, **kwargs)
def emit(self, record):
@@ -54,18 +56,30 @@ class MockLoggingHandler(logging.Handler):
Assert that the last line captured at the given level ends with
a particular substring.
"""
- methods = {'WARNING':'param.warning()', 'INFO':'param.message()',
- 'VERBOSE':'param.verbose()', 'DEBUG':'param.debug()'}
msg='\n\n{method}: {last_line}\ndoes not end with:\n{substring}'
last_line = self.tail(level, n=1)
if len(last_line) == 0:
- raise AssertionError('Missing {method} output: {repr(substring)}'.format(
- method=methods[level], substring=repr(substring)))
+ raise AssertionError('Missing {method} output: {substring}'.format(
+ method=self.param_methods[level], substring=repr(substring)))
if not last_line[0].endswith(substring):
- raise AssertionError(msg.format(method=methods[level],
+ raise AssertionError(msg.format(method=self.param_methods[level],
last_line=repr(last_line[0]),
substring=repr(substring)))
+ def assertContains(self, level, substring):
+ """
+ Assert that the last line captured at the given level contains a
+ particular substring.
+ """
+ msg='\n\n{method}: {last_line}\ndoes not contain:\n{substring}'
+ last_line = self.tail(level, n=1)
+ if len(last_line) == 0:
+ raise AssertionError('Missing {method} output: {substring}'.format(
+ method=self.param_methods[level], substring=repr(substring)))
+ if substring not in last_line[0]:
+ raise AssertionError(msg.format(method=self.param_methods[level],
+ last_line=repr(last_line[0]),
+ substring=repr(substring)))
class LoggingComparisonTestCase(ComparisonTestCase):
