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
1=== modified file 'u1testutils/logging.py'
2--- u1testutils/logging.py 2012-12-04 19:56:54 +0000
3+++ u1testutils/logging.py 2013-02-28 16:10:28 +0000
4@@ -25,11 +25,14 @@
5 """Just add the record to self.records."""
6 self.records.append(record)
7
8- def check(self, level, msg):
9+ def check(self, level, msg, check_traceback=False):
10 """Check that something is logged."""
11 for rec in self.records:
12 if rec.levelname == level and str(msg) in rec.getMessage():
13- return True
14+ if not check_traceback:
15+ return True
16+ elif rec.exc_text is not None:
17+ return True
18 return False
19
20 def setUp(self):
21@@ -44,7 +47,8 @@
22 self.root_logger.removeHandler(self.memento_handler)
23 super(LogHandlerTestCase, self).tearDown()
24
25- def assertLogLevelContains(self, level, message):
26+ def assertLogLevelContains(self, level, message, check_traceback=False):
27 # XXX michaeln 2010-08-18 Convert this to use testtools matchers
28 # for much better failure output.
29- self.assertTrue(self.memento_handler.check(level, message))
30+ self.assertTrue(self.memento_handler.check(
31+ level, message, check_traceback=check_traceback))
32
33=== added file 'u1testutils/selftests/unit/test_logging.py'
34--- u1testutils/selftests/unit/test_logging.py 1970-01-01 00:00:00 +0000
35+++ u1testutils/selftests/unit/test_logging.py 2013-02-28 16:10:28 +0000
36@@ -0,0 +1,29 @@
37+from mock import Mock
38+
39+from u1testutils.logging import LogHandlerTestCase
40+
41+
42+class LogHandlerTestCaseTestCase(LogHandlerTestCase):
43+
44+ def test_assertLogLevelContains_check_traceback(self):
45+ mock_record = Mock()
46+ mock_record.levelname = 'ERROR'
47+ mock_record.getMessage.return_value = 'some message'
48+ mock_record.exc_text = 'the exception'
49+
50+ self.memento_handler.emit(mock_record)
51+
52+ self.assertLogLevelContains('ERROR', 'some message',
53+ check_traceback=True)
54+
55+ def test_MementoHandler_check_check_traceback(self):
56+ mock_record = Mock()
57+ mock_record.levelname = 'ERROR'
58+ mock_record.getMessage.return_value = 'some message'
59+ mock_record.exc_text = 'the exception'
60+
61+ self.memento_handler.emit(mock_record)
62+
63+ result = self.memento_handler.check('ERROR', 'some message',
64+ check_traceback=True)
65+ self.assertTrue(result)

Subscribers

People subscribed via source and target branches

to all changes: