Merge lp:~canonical-platform-qa/autopilot/fix-bug-1414781 into lp:autopilot

Proposed by Sergio Cazzolato
Status: Needs review
Proposed branch: lp:~canonical-platform-qa/autopilot/fix-bug-1414781
Merge into: lp:autopilot
Diff against target: 266 lines (+54/-110)
4 files modified
autopilot/tests/__init__.py (+1/-57)
autopilot/tests/functional/test_input_stack.py (+10/-9)
autopilot/tests/unit/test_input.py (+12/-14)
autopilot/tests/unit/test_logging.py (+31/-30)
To merge this branch: bzr merge lp:~canonical-platform-qa/autopilot/fix-bug-1414781
Reviewer Review Type Date Requested Status
platform-qa-bot continuous-integration Needs Fixing
PS Jenkins bot continuous-integration Needs Fixing
Leo Arias (community) Needs Fixing
prod-platform-qa continuous-integration Pending
Sergio Cazzolato Pending
Review via email: mp+259276@code.launchpad.net

Commit message

autopilot.tests.unit.LogHandlerTestCase replaced by fixtures.FakeLogger

Description of the change

Bux #1414781 fixed

To post a comment you must log in.
Revision history for this message
Leo Arias (elopio) wrote :

Thanks Sergio. I left some comments in line.

Revision history for this message
Leo Arias (elopio) :
review: Needs Fixing
Revision history for this message
Sergio Cazzolato (sergio-j-cazzolato) :
Revision history for this message
Leo Arias (elopio) :
558. By Sergio Cazzolato

removing statements from with scope

Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :

FAILED: Continuous integration, rev:557
http://jenkins.qa.ubuntu.com/job/autopilot-ci/1072/
Executed test runs:

Click here to trigger a rebuild:
http://s-jenkins.ubuntu-ci:8080/job/autopilot-ci/1072/rebuild

review: Needs Fixing (continuous-integration)
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Needs Fixing (continuous-integration)
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Needs Fixing (continuous-integration)
Revision history for this message
platform-qa-bot (platform-qa-bot) wrote :
review: Approve (continuous-integration)
Revision history for this message
platform-qa-bot (platform-qa-bot) wrote :
review: Needs Fixing (continuous-integration)
Revision history for this message
platform-qa-bot (platform-qa-bot) wrote :
review: Needs Fixing (continuous-integration)
Revision history for this message
platform-qa-bot (platform-qa-bot) wrote :
review: Needs Fixing (continuous-integration)
Revision history for this message
platform-qa-bot (platform-qa-bot) wrote :
review: Needs Fixing (continuous-integration)
Revision history for this message
platform-qa-bot (platform-qa-bot) wrote :
review: Needs Fixing (continuous-integration)

Unmerged revisions

558. By Sergio Cazzolato

removing statements from with scope

557. By Sergio Cazzolato

LogHandlerTestCase removed, now using the FakeLogger as was suggested in the bug

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'autopilot/tests/__init__.py'
2--- autopilot/tests/__init__.py 2014-10-21 14:05:06 +0000
3+++ autopilot/tests/__init__.py 2015-05-21 21:20:45 +0000
4@@ -17,60 +17,4 @@
5 # along with this program. If not, see <http://www.gnu.org/licenses/>.
6 #
7
8-import logging
9-
10-import testtools
11-
12-
13-class LogHandlerTestCase(testtools.TestCase):
14-
15- """A mixin that adds a memento loghandler for testing logging."""
16-
17- class MementoHandler(logging.Handler):
18-
19- """A handler class which stores logging records in a list."""
20-
21- def __init__(self, *args, **kwargs):
22- """Create the instance, and add a records attribute."""
23- super().__init__(*args, **kwargs)
24- self.records = []
25-
26- def emit(self, record):
27- """Just add the record to self.records."""
28- self.records.append(record)
29-
30- def check(self, level, msg, check_traceback=False):
31- """Check that something is logged."""
32- result = False
33- for rec in self.records:
34- if rec.levelname == level:
35- result = str(msg) in rec.getMessage()
36- if not result and check_traceback:
37- result = str(msg) in rec.exc_text
38- if result:
39- break
40-
41- return result
42-
43- def setUp(self):
44- """Add the memento handler to the root logger."""
45- super().setUp()
46- self.memento_handler = self.MementoHandler()
47- self.root_logger = logging.getLogger()
48- self.root_logger.addHandler(self.memento_handler)
49-
50- def tearDown(self):
51- """Remove the memento handler from the root logger."""
52- self.root_logger.removeHandler(self.memento_handler)
53- super().tearDown()
54-
55- def assertLogLevelContains(self, level, message, check_traceback=False):
56- check = self.memento_handler.check(
57- level, message, check_traceback=check_traceback)
58-
59- msg = ('Expected logging message/s could not be found:\n%s\n'
60- 'Current logging records are:\n%s')
61- expected = '\t%s: %s' % (level, message)
62- records = ['\t%s: %s' % (r.levelname, r.getMessage())
63- for r in self.memento_handler.records]
64- self.assertTrue(check, msg % (expected, '\n'.join(records)))
65+
66
67=== modified file 'autopilot/tests/functional/test_input_stack.py'
68--- autopilot/tests/functional/test_input_stack.py 2015-05-08 04:32:51 +0000
69+++ autopilot/tests/functional/test_input_stack.py 2015-05-21 21:20:45 +0000
70@@ -17,7 +17,7 @@
71 # along with this program. If not, see <http://www.gnu.org/licenses/>.
72 #
73
74-
75+from fixtures import FakeLogger
76 import json
77 import logging
78 import os
79@@ -27,7 +27,8 @@
80 IsInstance,
81 Equals,
82 raises,
83- GreaterThan
84+ GreaterThan,
85+ Contains
86 )
87 from testscenarios import TestWithScenarios
88 from textwrap import dedent
89@@ -366,7 +367,7 @@
90 'window-mocker', window_spec_file, app_type='qt')
91
92
93-class MouseTestCase(AutopilotTestCase, tests.LogHandlerTestCase):
94+class MouseTestCase(AutopilotTestCase):
95
96 @skipIf(platform.model() != "Desktop", "Only suitable on Desktop (Mouse)")
97 def test_move_to_nonint_point(self):
98@@ -396,13 +397,13 @@
99
100 @skipIf(platform.model() != "Desktop", "Only suitable on Desktop (Mouse)")
101 def test_mouse_move_must_log_final_position_at_debug_level(self):
102- self.root_logger.setLevel(logging.DEBUG)
103+ log_format = "%(levelname)s:%(message)s"
104 mouse = Mouse.create()
105- mouse.move(10, 10)
106- self.assertLogLevelContains(
107- 'DEBUG',
108- "The mouse is now at position 10,10."
109- )
110+
111+ with FakeLogger(level=logging.DEBUG, format=log_format) as logger:
112+ mouse.move(10, 10)
113+
114+ self.assertThat(logger.output, Contains("DEBUG:The mouse is now at position 10,10."))
115
116
117 @skipIf(platform.model() != "Desktop", "Only suitable on Desktop (WinMocker)")
118
119=== modified file 'autopilot/tests/unit/test_input.py'
120--- autopilot/tests/unit/test_input.py 2015-03-22 06:20:40 +0000
121+++ autopilot/tests/unit/test_input.py 2015-05-21 21:20:45 +0000
122@@ -22,6 +22,7 @@
123 import testscenarios
124 from evdev import ecodes, uinput
125 from unittest.mock import ANY, call, Mock, patch
126+from fixtures import FakeLogger
127 from testtools import TestCase
128 from testtools.matchers import Contains, raises
129
130@@ -425,7 +426,7 @@
131 self.assert_expected_ev_abs('given_res_x', 'given_res_y', ev_abs)
132
133
134-class UInputTouchDeviceTestCase(tests.LogHandlerTestCase):
135+class UInputTouchDeviceTestCase(TestCase):
136 """Test the integration with evdev.UInput for the touch device."""
137
138 def setUp(self):
139@@ -583,19 +584,16 @@
140 touch, slot=first_slot, x=14, y=14)
141
142 def test_finger_move_must_log_position_at_debug_level(self):
143- self.root_logger.setLevel(logging.DEBUG)
144- touch = self.get_touch_with_mocked_backend()
145- touch.finger_down(0, 0)
146-
147- touch.finger_move(10, 10)
148- self.assertLogLevelContains(
149- 'DEBUG',
150- "Moving pointing 'finger' to position 10,10."
151- )
152- self.assertLogLevelContains(
153- 'DEBUG',
154- "The pointing 'finger' is now at position 10,10."
155- )
156+
157+ log_format = "%(levelname)s:%(message)s"
158+
159+ with FakeLogger(level=logging.DEBUG, format=log_format) as logger:
160+ touch = self.get_touch_with_mocked_backend()
161+ touch.finger_down(0, 0)
162+ touch.finger_move(10, 10)
163+
164+ self.assertThat(logger.output, Contains("DEBUG:Moving pointing 'finger' to position 10,10."))
165+ self.assertThat(logger.output, Contains("DEBUG:The pointing 'finger' is now at position 10,10."))
166
167 def test_finger_up_without_finger_pressed_must_raise_error(self):
168 touch = self.get_touch_with_mocked_backend()
169
170=== modified file 'autopilot/tests/unit/test_logging.py'
171--- autopilot/tests/unit/test_logging.py 2014-10-06 17:04:21 +0000
172+++ autopilot/tests/unit/test_logging.py 2015-05-21 21:20:45 +0000
173@@ -19,14 +19,14 @@
174
175 import logging
176
177+from fixtures import FakeLogger
178+from testtools import TestCase
179+from testtools.matchers import Contains
180 from unittest.mock import Mock
181-import testtools
182
183-from autopilot import tests
184 from autopilot.logging import log_action
185 from autopilot._logging import TestCaseLoggingFixture
186
187-
188 class ObjectWithLogDecorator(object):
189
190 @log_action(logging.info)
191@@ -47,47 +47,48 @@
192 pass
193
194
195-class LoggingTestCase(tests.LogHandlerTestCase):
196+class LoggingTestCase(TestCase):
197
198 def setUp(self):
199 super(LoggingTestCase, self).setUp()
200- self.root_logger.setLevel(logging.INFO)
201 self.logged_object = ObjectWithLogDecorator()
202+ self.log_format = "%(levelname)s:%(message)s"
203
204 def test_logged_action_without_docstring(self):
205- self.logged_object.do_something_without_docstring(
206- 'arg1', 'arg2', arg3='arg3', arg4='arg4')
207- self.assertLogLevelContains(
208- 'INFO',
209- "ObjectWithLogDecorator: do_something_without_docstring. "
210- "Arguments ('arg1', 'arg2'). "
211- "Keyword arguments: {'arg3': 'arg3', 'arg4': 'arg4'}.")
212+ with FakeLogger(level=logging.INFO, format=self.log_format) as logger:
213+ self.logged_object.do_something_without_docstring('arg1', 'arg2', arg3='arg3', arg4='arg4')
214+
215+ self.assertThat(logger.output, Contains("INFO:"
216+ "ObjectWithLogDecorator: do_something_without_docstring. "
217+ "Arguments ('arg1', 'arg2'). "
218+ "Keyword arguments: {'arg3': 'arg3', 'arg4': 'arg4'}."))
219
220 def test_logged_action_with_docstring(self):
221- self.logged_object.do_something_with_docstring(
222- 'arg1', 'arg2', arg3='arg3', arg4='arg4')
223- self.assertLogLevelContains(
224- 'INFO',
225- "ObjectWithLogDecorator: Do something with docstring. "
226- "Arguments ('arg1', 'arg2'). "
227- "Keyword arguments: {'arg3': 'arg3', 'arg4': 'arg4'}.")
228+ with FakeLogger(level=logging.INFO, format=self.log_format) as logger:
229+ self.logged_object.do_something_with_docstring('arg1', 'arg2', arg3='arg3', arg4='arg4')
230+
231+ self.assertThat(logger.output, Contains("INFO:"
232+ "ObjectWithLogDecorator: Do something with docstring. "
233+ "Arguments ('arg1', 'arg2'). "
234+ "Keyword arguments: {'arg3': 'arg3', 'arg4': 'arg4'}."))
235
236 def test_logged_action_with_multiline_docstring(self):
237- self.logged_object.do_something_with_multiline_docstring(
238- 'arg1', 'arg2', arg3='arg3', arg4='arg4')
239- self.assertLogLevelContains(
240- 'INFO',
241- "ObjectWithLogDecorator: "
242- "Do something with a multiline docstring. "
243- "Arguments ('arg1', 'arg2'). "
244- "Keyword arguments: {'arg3': 'arg3', 'arg4': 'arg4'}.")
245-
246-
247-class TestCaseLoggingFixtureTests(testtools.TestCase):
248+ with FakeLogger(level=logging.INFO, format=self.log_format) as logger:
249+ self.logged_object.do_something_with_multiline_docstring('arg1', 'arg2', arg3='arg3', arg4='arg4')
250+
251+ self.assertThat(logger.output, Contains("INFO:"
252+ "ObjectWithLogDecorator: "
253+ "Do something with a multiline docstring. "
254+ "Arguments ('arg1', 'arg2'). "
255+ "Keyword arguments: {'arg3': 'arg3', 'arg4': 'arg4'}."))
256+
257+
258+class TestCaseLoggingFixtureTests(TestCase):
259
260 def test_test_log_is_added(self):
261 token = self.getUniqueString()
262 add_detail_fn = Mock()
263+
264 fixture = TestCaseLoggingFixture("Test.id", add_detail_fn)
265 fixture.setUp()
266 logging.getLogger(__name__).info(token)

Subscribers

People subscribed via source and target branches