Merge lp:~ricardokirkner/u1-test-utils/assert-log-with-traceback into lp:u1-test-utils

Proposed by Ricardo Kirkner
Status: Merged
Approved by: Ricardo Kirkner
Approved revision: 37
Merged at revision: 35
Proposed branch: lp:~ricardokirkner/u1-test-utils/assert-log-with-traceback
Merge into: lp:u1-test-utils
Diff against target: 65 lines (+37/-4)
2 files modified
u1testutils/logging.py (+8/-4)
u1testutils/selftests/unit/test_logging.py (+29/-0)
To merge this branch: bzr merge lp:~ricardokirkner/u1-test-utils/assert-log-with-traceback
Reviewer Review Type Date Requested Status
Leo Arias (community) code review Approve
Review via email: mp+151032@code.launchpad.net

Commit message

Allow assertLogLevelContains to also check if the log record contains traceback information.

Description of the change

Allow assertLogLevelContains to also check if the log record contains traceback information.

To post a comment you must log in.
36. By Ricardo Kirkner

renamed has_traceback to check_traceback

37. By Ricardo Kirkner

fix per code review

Revision history for this message
Leo Arias (elopio) wrote :

<elopio> pindonga: shouldn't this parameter has_traceback=False be called check_traceback ?
<pindonga> I can change that if that name is better
<elopio> pindonga: when I read it, I thought that it was going to fail if has_traceback=False and there was a traceback on the log entry.
<pindonga> k
<elopio> or the contrary. But has_traceback=False only ignores the traceback check. So for me, that would be a better name.
<elopio> for the rest, +1
<elopio> pindonga: oh, and also on line 16, I would find it clearer with an elif.
<pindonga> it's exactly the same, yes? bc of the return
<pindonga> but I can change that if it makes you happier
<pindonga> :)
<pindonga> elopio: revno 37 pushed
<elopio> pindonga: yes, it's exactly the same. But it takes one less second to notice that you will never enter in that code block. That's why I said "I find it clearer", it also means, feel free to ignore me whenever you want :)

review: Approve (code review)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'u1testutils/logging.py'
--- u1testutils/logging.py 2012-12-04 19:56:54 +0000
+++ u1testutils/logging.py 2013-02-28 16:10:28 +0000
@@ -25,11 +25,14 @@
25 """Just add the record to self.records."""25 """Just add the record to self.records."""
26 self.records.append(record)26 self.records.append(record)
2727
28 def check(self, level, msg):28 def check(self, level, msg, check_traceback=False):
29 """Check that something is logged."""29 """Check that something is logged."""
30 for rec in self.records:30 for rec in self.records:
31 if rec.levelname == level and str(msg) in rec.getMessage():31 if rec.levelname == level and str(msg) in rec.getMessage():
32 return True32 if not check_traceback:
33 return True
34 elif rec.exc_text is not None:
35 return True
33 return False36 return False
3437
35 def setUp(self):38 def setUp(self):
@@ -44,7 +47,8 @@
44 self.root_logger.removeHandler(self.memento_handler)47 self.root_logger.removeHandler(self.memento_handler)
45 super(LogHandlerTestCase, self).tearDown()48 super(LogHandlerTestCase, self).tearDown()
4649
47 def assertLogLevelContains(self, level, message):50 def assertLogLevelContains(self, level, message, check_traceback=False):
48 # XXX michaeln 2010-08-18 Convert this to use testtools matchers51 # XXX michaeln 2010-08-18 Convert this to use testtools matchers
49 # for much better failure output.52 # for much better failure output.
50 self.assertTrue(self.memento_handler.check(level, message))53 self.assertTrue(self.memento_handler.check(
54 level, message, check_traceback=check_traceback))
5155
=== added file 'u1testutils/selftests/unit/test_logging.py'
--- u1testutils/selftests/unit/test_logging.py 1970-01-01 00:00:00 +0000
+++ u1testutils/selftests/unit/test_logging.py 2013-02-28 16:10:28 +0000
@@ -0,0 +1,29 @@
1from mock import Mock
2
3from u1testutils.logging import LogHandlerTestCase
4
5
6class LogHandlerTestCaseTestCase(LogHandlerTestCase):
7
8 def test_assertLogLevelContains_check_traceback(self):
9 mock_record = Mock()
10 mock_record.levelname = 'ERROR'
11 mock_record.getMessage.return_value = 'some message'
12 mock_record.exc_text = 'the exception'
13
14 self.memento_handler.emit(mock_record)
15
16 self.assertLogLevelContains('ERROR', 'some message',
17 check_traceback=True)
18
19 def test_MementoHandler_check_check_traceback(self):
20 mock_record = Mock()
21 mock_record.levelname = 'ERROR'
22 mock_record.getMessage.return_value = 'some message'
23 mock_record.exc_text = 'the exception'
24
25 self.memento_handler.emit(mock_record)
26
27 result = self.memento_handler.check('ERROR', 'some message',
28 check_traceback=True)
29 self.assertTrue(result)

Subscribers

People subscribed via source and target branches

to all changes: