Commits

Yuya Nishihara committed 6e8ef12

thgbackdoor: use unittest.TestCase to reduce redundant setup/teardown codes

Comments (0)

Files changed (1)

tests/thgbackdoor_test.py

-import errno, os, shutil, socket, subprocess, sys, tempfile, time
+import errno, os, shutil, socket, subprocess, sys, tempfile, time, unittest
 try:
     import cPickle as pickle
 except ImportError:
     def __getattr__(self, name):
         return getattr(self._controller, name)
 
-def test_run_workbench():
-    thg = ThgClient(BACKDOOR_ADDRESS, timeout=5)
-    try:
-        assert thg.eval('dlg').startswith('<tortoisehg.hgqt.workbench')
-    finally:
-        thg.close()
+class ThgBackDoorTest(unittest.TestCase):
+    def setUp(self):
+        self.thg = ThgClient(BACKDOOR_ADDRESS, timeout=5)
 
-def test_control_by_qtest():
-    thg = ThgClient(BACKDOOR_ADDRESS, timeout=5)
-    try:
-        thg.exec_('''
+    def tearDown(self):
+        self.thg.close()
+
+    def test_run_workbench(self):
+        self.assertTrue(
+            self.thg.eval('dlg').startswith('<tortoisehg.hgqt.workbench'))
+
+    def test_control_by_qtest(self):
+        self.thg.exec_('''
         from PyQt4.QtTest import QTest
         QTest.qWaitForWindowShown(dlg)
         QTest.keyClick(dlg, "N", Qt.ControlModifier)
         # introduce a helper function?
         for _i in xrange(1):
             time.sleep(0.1)
-            aw = thg.eval('QApplication.activeWindow().__class__.__name__')
+            aw = self.thg.eval('QApplication.activeWindow().__class__.__name__')
             if aw == 'InitDialog':
                 break
         else:
-            raise AssertionError('failed to exec InitDialog')
-    finally:
-        thg.close()
+            self.fail('failed to exec InitDialog')
 
-@timed(5)
-def test_timeout():
-    thg = ThgClient(BACKDOOR_ADDRESS, timeout=1)
-    try:
-        assert_raises(socket.timeout, lambda: thg.wait())
-    finally:
-        thg.close()
+    def test_timeout(self):
+        self.assertRaises(socket.timeout, lambda: self.thg.wait())
 
-def test_raise_exception():
-    thg = ThgClient(BACKDOOR_ADDRESS, timeout=5)
-    try:
-        assert_raises(RemoteError,
-                      lambda: thg.exec_('raise Exception("oops")'))
-    finally:
-        thg.close()
+    def test_raise_exception(self):
+        self.assertRaises(RemoteError,
+                          lambda: self.thg.exec_('raise Exception("oops")'))
 
-def test_raise_delayed_exception():
-    thg = ThgClient(BACKDOOR_ADDRESS, timeout=5)
-    try:
-        thg.exec_('QTimer.singleShot(0, lambda: _unknown_attribute)')
-        assert_raises(RemoteError, lambda: thg.wait())
-    finally:
-        thg.close()
+    def test_raise_delayed_exception(self):
+        self.thg.exec_('QTimer.singleShot(0, lambda: _unknown_attribute)')
+        self.assertRaises(RemoteError, lambda: self.thg.wait())
 
 # TODO: test unix domain socket?