Rev 5343: (mbp) change some test tearDowns to addCleanup or overrideAttr (Martin Pool) in file:///home/pqm/archives/thelove/bzr/%2Btrunk/

Canonical.com Patch Queue Manager pqm at pqm.ubuntu.com
Tue Jul 13 20:02:15 BST 2010


At file:///home/pqm/archives/thelove/bzr/%2Btrunk/

------------------------------------------------------------
revno: 5343 [merge]
revision-id: pqm at pqm.ubuntu.com-20100713190212-bnayd5moplwtxhhb
parent: pqm at pqm.ubuntu.com-20100713090648-fiw7vgel62e97c1l
parent: mbp at canonical.com-20100713164155-7q4g3tf5wawzji6w
committer: Canonical.com Patch Queue Manager <pqm at pqm.ubuntu.com>
branch nick: +trunk
timestamp: Tue 2010-07-13 20:02:12 +0100
message:
  (mbp) change some test tearDowns to addCleanup or overrideAttr (Martin Pool)
modified:
  bzrlib/tests/blackbox/test_status.py teststatus.py-20050712014354-508855eb9f29f7dc
  bzrlib/tests/test_http.py      testhttp.py-20051018020158-b2eef6e867c514d9
  bzrlib/tests/test_remote.py    test_remote.py-20060720103555-yeeg2x51vn0rbtdp-2
  bzrlib/tests/test_smart_request.py test_smart_request.p-20090211070731-o38wayv3asm25d6a-1
  doc/developers/testing.txt     testing.txt-20080812140359-i70zzh6v2z7grqex-1
=== modified file 'bzrlib/tests/blackbox/test_status.py'
--- a/bzrlib/tests/blackbox/test_status.py	2010-04-07 21:34:13 +0000
+++ b/bzrlib/tests/blackbox/test_status.py	2010-06-24 06:57:31 +0000
@@ -704,16 +704,6 @@
 
 class TestStatusEncodings(TestCaseWithTransport):
 
-    def setUp(self):
-        TestCaseWithTransport.setUp(self)
-        self.user_encoding = osutils._cached_user_encoding
-        self.stdout = sys.stdout
-
-    def tearDown(self):
-        osutils._cached_user_encoding = self.user_encoding
-        sys.stdout = self.stdout
-        TestCaseWithTransport.tearDown(self)
-
     def make_uncommitted_tree(self):
         """Build a branch with uncommitted unicode named changes in the cwd."""
         working_tree = self.make_branch_and_tree(u'.')
@@ -727,8 +717,7 @@
         return working_tree
 
     def test_stdout_ascii(self):
-        sys.stdout = StringIO()
-        osutils._cached_user_encoding = 'ascii'
+        self.overrideAttr(osutils, '_cached_user_encoding', 'ascii')
         working_tree = self.make_uncommitted_tree()
         stdout, stderr = self.run_bzr("status")
 
@@ -738,8 +727,7 @@
 """)
 
     def test_stdout_latin1(self):
-        sys.stdout = StringIO()
-        osutils._cached_user_encoding = 'latin-1'
+        self.overrideAttr(osutils, '_cached_user_encoding', 'latin-1')
         working_tree = self.make_uncommitted_tree()
         stdout, stderr = self.run_bzr('status')
 

=== modified file 'bzrlib/tests/test_http.py'
--- a/bzrlib/tests/test_http.py	2010-06-11 10:59:23 +0000
+++ b/bzrlib/tests/test_http.py	2010-07-13 19:02:12 +0000
@@ -1125,10 +1125,7 @@
     def setUp(self):
         tests.TestCase.setUp(self)
         self._old_env = {}
-
-    def tearDown(self):
-        self._restore_env()
-        tests.TestCase.tearDown(self)
+        self.addCleanup(self._restore_env)
 
     def _install_env(self, env):
         for name, value in env.iteritems():
@@ -1974,13 +1971,8 @@
         # We override at class level because constructors may propagate the
         # bound method and render instance overriding ineffective (an
         # alternative would be to define a specific ui factory instead...)
-        self.orig_report_activity = self._transport._report_activity
-        self._transport._report_activity = report_activity
-
-    def tearDown(self):
-        self._transport._report_activity = self.orig_report_activity
-        self.server.stop_server()
-        tests.TestCase.tearDown(self)
+        self.overrideAttr(self._transport, '_report_activity', report_activity)
+        self.addCleanup(self.server.stop_server)
 
     def get_transport(self):
         return self._transport(self.server.get_url())
@@ -2103,51 +2095,22 @@
 class TestActivity(tests.TestCase, TestActivityMixin):
 
     def setUp(self):
-        tests.TestCase.setUp(self)
-        self.server = self._activity_server(self._protocol_version)
-        self.server.start_server()
-        self.activities = {}
-        def report_activity(t, bytes, direction):
-            count = self.activities.get(direction, 0)
-            count += bytes
-            self.activities[direction] = count
-
-        # We override at class level because constructors may propagate the
-        # bound method and render instance overriding ineffective (an
-        # alternative would be to define a specific ui factory instead...)
-        self.orig_report_activity = self._transport._report_activity
-        self._transport._report_activity = report_activity
-
-    def tearDown(self):
-        self._transport._report_activity = self.orig_report_activity
-        self.server.stop_server()
-        tests.TestCase.tearDown(self)
+        TestActivityMixin.setUp(self)
 
 
 class TestNoReportActivity(tests.TestCase, TestActivityMixin):
 
+    # Unlike TestActivity, we are really testing ReportingFileSocket and
+    # ReportingSocket, so we don't need all the parametrization. Since
+    # ReportingFileSocket and ReportingSocket are wrappers, it's easier to
+    # test them through their use by the transport than directly (that's a
+    # bit less clean but far more simpler and effective).
+    _activity_server = ActivityHTTPServer
+    _protocol_version = 'HTTP/1.1'
+
     def setUp(self):
-        tests.TestCase.setUp(self)
-        # Unlike TestActivity, we are really testing ReportingFileSocket and
-        # ReportingSocket, so we don't need all the parametrization. Since
-        # ReportingFileSocket and ReportingSocket are wrappers, it's easier to
-        # test them through their use by the transport than directly (that's a
-        # bit less clean but far more simpler and effective).
-        self.server = ActivityHTTPServer('HTTP/1.1')
-        self._transport=_urllib.HttpTransport_urllib
-
-        self.server.start_server()
-
-        # We override at class level because constructors may propagate the
-        # bound method and render instance overriding ineffective (an
-        # alternative would be to define a specific ui factory instead...)
-        self.orig_report_activity = self._transport._report_activity
-        self._transport._report_activity = None
-
-    def tearDown(self):
-        self._transport._report_activity = self.orig_report_activity
-        self.server.stop_server()
-        tests.TestCase.tearDown(self)
+        self._transport =_urllib.HttpTransport_urllib
+        TestActivityMixin.setUp(self)
 
     def assertActivitiesMatch(self):
         # Nothing to check here

=== modified file 'bzrlib/tests/test_remote.py'
--- a/bzrlib/tests/test_remote.py	2010-06-20 11:18:38 +0000
+++ b/bzrlib/tests/test_remote.py	2010-07-13 19:02:12 +0000
@@ -89,10 +89,7 @@
         self.transport = self.get_transport()
         # make a branch that can be opened over the smart transport
         self.local_wt = BzrDir.create_standalone_workingtree('.')
-
-    def tearDown(self):
-        self.transport.disconnect()
-        tests.TestCaseWithTransport.tearDown(self)
+        self.addCleanup(self.transport.disconnect)
 
     def test_create_remote_bzrdir(self):
         b = remote.RemoteBzrDir(self.transport, remote.RemoteBzrDirFormat())

=== modified file 'bzrlib/tests/test_smart_request.py'
--- a/bzrlib/tests/test_smart_request.py	2010-06-20 11:18:38 +0000
+++ b/bzrlib/tests/test_smart_request.py	2010-07-13 19:02:12 +0000
@@ -187,9 +187,11 @@
 
 class TestJailHook(TestCaseWithMemoryTransport):
 
-    def tearDown(self):
-        request.jail_info.transports = None
-        TestCaseWithMemoryTransport.tearDown(self)
+    def setUp(self):
+        super(TestJailHook, self).setUp()
+        def clear_jail_info():
+            request.jail_info.transports = None
+        self.addCleanup(clear_jail_info)
 
     def test_jail_hook(self):
         request.jail_info.transports = None

=== modified file 'doc/developers/testing.txt'
--- a/doc/developers/testing.txt	2010-06-11 06:19:48 +0000
+++ b/doc/developers/testing.txt	2010-06-24 07:11:55 +0000
@@ -870,6 +870,25 @@
 Please see bzrlib.treebuilder for more details.
 
 
+Temporarily changing state
+~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+If your test needs to temporarily mutate some global state, and you need
+it restored at the end, you can say for example::
+
+    self.overrideAttr(osutils, '_cached_user_encoding', 'latin-1')
+
+Cleaning up
+~~~~~~~~~~~
+
+Our base ``TestCase`` class provides an ``addCleanup`` method, which
+should be used instead of ``tearDown``.  All the cleanups are run when the
+test finishes, regardless of whether it passes or fails.  If one cleanup
+fails, later cleanups are still run.
+
+(The same facility is available outside of tests through
+``bzrlib.cleanup``.)
+
 .. |--| unicode:: U+2014
 
 ..




More information about the bazaar-commits mailing list