Merge lp:~frankban/launchpad/bug-993510 into lp:launchpad

Proposed by Francesco Banconi
Status: Merged
Approved by: Brad Crittenden
Approved revision: no longer in the source branch.
Merged at revision: 15318
Proposed branch: lp:~frankban/launchpad/bug-993510
Merge into: lp:launchpad
Diff against target: 51 lines (+18/-1)
2 files modified
lib/lp/testing/__init__.py (+1/-1)
lib/lp/testing/tests/test_testcase.py (+17/-0)
To merge this branch: bzr merge lp:~frankban/launchpad/bug-993510
Reviewer Review Type Date Requested Status
Benji York (community) code Approve
Review via email: mp+107630@code.launchpad.net

Description of the change

= Summary =

Log handlers are removed before each test because tests should not depend on global logging config.

== Changes ==

Iterate over a copy of the handler list, so that the original list is not affected by the .remove calls and all handlers are correctly removed.

NO QA

== Lint ==

Checking for conflicts and issues in changed files.

Linting changed files:
  lib/lp/testing/__init__.py

== Tests ==

$ bin/test -vvc --load-list ../s6
Running tests at level 1
Running lp.testing.layers.LaunchpadZopelessLayer tests:
  Set up lp.testing.layers.BaseLayer in 0.052 seconds.
  Set up lp.testing.layers.ZopelessLayer in 3.405 seconds.
  Set up lp.testing.layers.DatabaseLayer in 1.217 seconds.
  Set up lp.testing.layers.LibrarianLayer in 5.545 seconds.
  Set up lp.testing.layers.MemcachedLayer in 0.113 seconds.
  Set up lp.testing.layers.RabbitMQLayer in 1.501 seconds.
  Set up lp.testing.layers.LaunchpadLayer in 0.000 seconds.
  Set up lp.testing.layers.LaunchpadScriptLayer in 0.003 seconds.
  Set up lp.testing.layers.LaunchpadZopelessLayer in 0.000 seconds.
  Running:
 lp.archivepublisher.tests.test_publish_ftpmaster.TestPublishFTPMasterScript.test_script_is_happy_with_no_publications
 lp.services.job.tests.test_runner.TestJobRunner.test_runJobHandleErrors_oops_generated_user_notify_failsNo handlers could be found for logger "root"

  Ran 2 tests with 0 failures and 0 errors in 2.726 seconds.
Tearing down left over layers:
  Tear down lp.testing.layers.LaunchpadZopelessLayer in 0.000 seconds.
  Tear down lp.testing.layers.LaunchpadScriptLayer in 0.001 seconds.
  Tear down lp.testing.layers.ZopelessLayer ... not supported
  Tear down lp.testing.layers.LaunchpadLayer in 0.000 seconds.
  Tear down lp.testing.layers.LibrarianLayer in 0.137 seconds.
  Tear down lp.testing.layers.MemcachedLayer in 0.001 seconds.
  Tear down lp.testing.layers.RabbitMQLayer ... not supported
  Tear down lp.testing.layers.DatabaseLayer in 0.162 seconds.
  Tear down lp.testing.layers.BaseLayer in 0.001 seconds.

To post a comment you must log in.
Revision history for this message
Benji York (benji) wrote :

Looks good. I see why this would be a problem.

I wonder if we should have a test for this behaviour.

review: Approve (code)
Revision history for this message
Francesco Banconi (frankban) wrote :

Thanks Benji, added a test as you suggested.

$ bin/test -cvvt lp.testing.tests.test_testcase.TestRemoveLoggingHandlers
Running tests at level 1
Running zope.testing.testrunner.layer.UnitTests tests:
  Set up zope.testing.testrunner.layer.UnitTests in 0.000 seconds.
  Running:
 lp.testing.tests.test_testcase.TestRemoveLoggingHandlers.test_handlers_list_is_empty
  Ran 1 tests with 0 failures and 0 errors in 0.001 seconds.
Tearing down left over layers:
  Tear down zope.testing.testrunner.layer.UnitTests in 0.000 seconds.

Revision history for this message
Benji York (benji) wrote :

The test looks good. It is sort of funny (in a good way) how the test machinery is part of the test itself.

review: Approve (code)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'lib/lp/testing/__init__.py'
--- lib/lp/testing/__init__.py 2012-05-28 08:44:41 +0000
+++ lib/lp/testing/__init__.py 2012-05-28 15:45:24 +0000
@@ -653,7 +653,7 @@
653 # Remove all log handlers, tests should not depend on global logging653 # Remove all log handlers, tests should not depend on global logging
654 # config but should make their own config instead.654 # config but should make their own config instead.
655 logger = logging.getLogger()655 logger = logging.getLogger()
656 for handler in logger.handlers:656 for handler in list(logger.handlers):
657 logger.removeHandler(handler)657 logger.removeHandler(handler)
658658
659 def assertStatementCount(self, expected_count, function, *args, **kwargs):659 def assertStatementCount(self, expected_count, function, *args, **kwargs):
660660
=== modified file 'lib/lp/testing/tests/test_testcase.py'
--- lib/lp/testing/tests/test_testcase.py 2012-01-01 18:15:02 +0000
+++ lib/lp/testing/tests/test_testcase.py 2012-05-28 15:45:24 +0000
@@ -5,6 +5,7 @@
55
6__metaclass__ = type6__metaclass__ = type
77
8import logging
8from StringIO import StringIO9from StringIO import StringIO
9import sys10import sys
1011
@@ -16,6 +17,7 @@
16from lp.services.webapp import errorlog17from lp.services.webapp import errorlog
17from lp.testing import (18from lp.testing import (
18 record_statements,19 record_statements,
20 TestCase,
19 TestCaseWithFactory,21 TestCaseWithFactory,
20 )22 )
21from lp.testing.layers import (23from lp.testing.layers import (
@@ -102,3 +104,18 @@
102 # rfc822 serializer is lossy).104 # rfc822 serializer is lossy).
103 oops_report = self.oopses[0]105 oops_report = self.oopses[0]
104 self.assertEqual(from_details['id'], oops_report['id'])106 self.assertEqual(from_details['id'], oops_report['id'])
107
108
109class TestRemoveLoggingHandlers(TestCase):
110
111 def setUp(self):
112 self.logger = logging.getLogger()
113 # Add 2 handlers.
114 self.logger.addHandler(logging.Handler())
115 self.logger.addHandler(logging.Handler())
116 # `TestCase.setUp()` removes the handlers just added.
117 super(TestRemoveLoggingHandlers, self).setUp()
118
119 def test_handlers_list_is_empty(self):
120 # Ensure `TestCase.setUp()` correctly removed all logging handlers.
121 self.assertEqual(0, len(self.logger.handlers))