]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
use "with self.assertRaises" pattern
authorAkihiro Yamazaki <yamazaki@iij.ad.jp>
Thu, 27 Mar 2014 15:27:39 +0000 (00:27 +0900)
committerAkihiro Yamazaki <yamazaki@iij.ad.jp>
Fri, 28 Mar 2014 00:10:58 +0000 (09:10 +0900)
tornado/test/concurrent_test.py
tornado/test/httpclient_test.py
tornado/test/ioloop_test.py
tornado/test/template_test.py
tornado/test/testing_test.py
tornado/test/util_test.py

index 2506ef45e05a782ca48c7f807605a50d09d8789f..edd70c9a7f275b93ec4589f490bf818d94da7067 100644 (file)
@@ -163,11 +163,9 @@ class ReturnFutureTest(AsyncTestCase):
                 self.expected_frame = traceback.extract_tb(
                     sys.exc_info()[2], limit=1)[0]
                 raise
-        try:
+        with self.assertRaises(ZeroDivisionError):
             yield f()
-            self.fail("didn't get expected exception")
-        except ZeroDivisionError:
-            tb = traceback.extract_tb(sys.exc_info()[2])
+        tb = traceback.extract_tb(sys.exc_info()[2])
         self.assertIn(self.expected_frame, tb)
 
 # The following series of classes demonstrate and test various styles
index 984f5e0c579ce2745ffc4045ce8fcc8350dcbdbe..78daa74dccbf413027c53aaa00a6cea1dfbaf9db 100644 (file)
@@ -356,12 +356,10 @@ Transfer-Encoding: chunked
 
     @gen_test
     def test_future_http_error(self):
-        try:
+        with self.assertRaises(HTTPError) as context:
             yield self.http_client.fetch(self.get_url('/notfound'))
-        except HTTPError as e:
-            got_exception = e
-        self.assertEqual(got_exception.code, 404)
-        self.assertEqual(got_exception.response.code, 404)
+        self.assertEqual(context.exception.code, 404)
+        self.assertEqual(context.exception.response.code, 404)
 
     @gen_test
     def test_reuse_request_from_response(self):
index bd069c2497878b91acaf569851dba2bf4b9ac61d..b7d1f3a69b8a6b2886dc55d680444e424ca7beb3 100644 (file)
@@ -112,13 +112,9 @@ class TestIOLoop(AsyncTestCase):
         thread = threading.Thread(target=target)
         thread.start()
         closing.wait()
-        for i in range(1000):
-            try:
+        with self.assertRaisesRegexp(RuntimeError, "\AIOLoop is closing\Z"):
+            for i in range(1000):
                 other_ioloop.add_callback(lambda: None)
-            except RuntimeError as e:
-                got_exception = e
-                break
-        self.assertEqual("IOLoop is closing", str(got_exception))
 
     def test_handle_callback_exception(self):
         # IOLoop.handle_callback_exception can be overridden to catch
index c1b1e48eca3d21574d192f375fe07a850db6fc44..181008dc50b6dc947651eafb3d50e6d5b10a920c 100644 (file)
@@ -149,20 +149,14 @@ try{% set y = 1/x %}
         self.assertEqual(result, b"013456")
 
     def test_break_outside_loop(self):
-        try:
+        with self.assertRaises(ParseError):
             Template(utf8("{% break %}"))
-            raise Exception("Did not get expected exception")
-        except ParseError:
-            pass
 
     def test_break_in_apply(self):
         # This test verifies current behavior, although of course it would
         # be nice if apply didn't cause seemingly unrelated breakage
-        try:
+        with self.assertRaises(ParseError):
             Template(utf8("{% for i in [] %}{% apply foo %}{% break %}{% end %}{% end %}"))
-            raise Exception("Did not get expected exception")
-        except ParseError:
-            pass
 
     @unittest.skipIf(sys.version_info >= division.getMandatoryRelease(),
                      'no testable future imports')
@@ -180,10 +174,9 @@ class StackTraceTest(unittest.TestCase):
 two{{1/0}}
 three
         """})
-        try:
+        with self.assertRaises(ZeroDivisionError):
             loader.load("test.html").generate()
-        except ZeroDivisionError:
-            exc_stack = traceback.format_exc()
+        exc_stack = traceback.format_exc()
         self.assertTrue("# test.html:2" in exc_stack)
 
     def test_error_line_number_directive(self):
@@ -191,10 +184,9 @@ three
 two{%if 1/0%}
 three{%end%}
         """})
-        try:
+        with self.assertRaises(ZeroDivisionError):
             loader.load("test.html").generate()
-        except ZeroDivisionError:
-            exc_stack = traceback.format_exc()
+        exc_stack = traceback.format_exc()
         self.assertTrue("# test.html:2" in exc_stack)
 
     def test_error_line_number_module(self):
@@ -202,10 +194,9 @@ three{%end%}
             "base.html": "{% module Template('sub.html') %}",
             "sub.html": "{{1/0}}",
         }, namespace={"_tt_modules": ObjectDict({"Template": lambda path, **kwargs: loader.load(path).generate(**kwargs)})})
-        try:
+        with self.assertRaises(ZeroDivisionError):
             loader.load("base.html").generate()
-        except ZeroDivisionError:
-            exc_stack = traceback.format_exc()
+        exc_stack = traceback.format_exc()
         self.assertTrue('# base.html:1' in exc_stack)
         self.assertTrue('# sub.html:1' in exc_stack)
 
@@ -214,10 +205,9 @@ three{%end%}
             "base.html": "{% include 'sub.html' %}",
             "sub.html": "{{1/0}}",
         })
-        try:
+        with self.assertRaises(ZeroDivisionError):
             loader.load("base.html").generate()
-        except ZeroDivisionError:
-            exc_stack = traceback.format_exc()
+        exc_stack = traceback.format_exc()
         self.assertTrue("# sub.html:1 (via base.html:1)" in exc_stack)
 
     def test_error_line_number_extends_base_error(self):
@@ -225,10 +215,9 @@ three{%end%}
             "base.html": "{{1/0}}",
             "sub.html": "{% extends 'base.html' %}",
         })
-        try:
+        with self.assertRaises(ZeroDivisionError):
             loader.load("sub.html").generate()
-        except ZeroDivisionError:
-            exc_stack = traceback.format_exc()
+        exc_stack = traceback.format_exc()
         self.assertTrue("# base.html:1" in exc_stack)
 
     def test_error_line_number_extends_sub_error(self):
@@ -240,10 +229,9 @@ three{%end%}
 {{1/0}}
 {% end %}
             """})
-        try:
+        with self.assertRaises(ZeroDivisionError):
             loader.load("sub.html").generate()
-        except ZeroDivisionError:
-            exc_stack = traceback.format_exc()
+        exc_stack = traceback.format_exc()
         self.assertTrue("# sub.html:4 (via base.html:1)" in exc_stack)
 
     def test_multi_includes(self):
@@ -252,10 +240,9 @@ three{%end%}
             "b.html": "{% include 'c.html' %}",
             "c.html": "{{1/0}}",
         })
-        try:
+        with self.assertRaises(ZeroDivisionError):
             loader.load("a.html").generate()
-        except ZeroDivisionError:
-            exc_stack = traceback.format_exc()
+        exc_stack = traceback.format_exc()
         self.assertTrue("# c.html:1 (via b.html:1, a.html:1)" in exc_stack)
 
 
index ef545e762a5e7c35cadc6980a5cb4d27d1253336..92eb9a48d3bc80f34e2011248822b84c7ea61fb7 100644 (file)
@@ -28,11 +28,8 @@ def set_environ(name, value):
 class AsyncTestCaseTest(AsyncTestCase):
     def test_exception_in_callback(self):
         self.io_loop.add_callback(lambda: 1 / 0)
-        try:
+        with self.assertRaises(ZeroDivisionError):
             self.wait()
-            self.fail("did not get expected exception")
-        except ZeroDivisionError:
-            pass
 
     def test_wait_timeout(self):
         time = self.io_loop.time
@@ -151,11 +148,9 @@ class GenTest(AsyncTestCase):
 
         # This can't use assertRaises because we need to inspect the
         # exc_info triple (and not just the exception object)
-        try:
+        with self.assertRaises(ioloop.TimeoutError):
             test(self)
-            self.fail("did not get expected exception")
-        except ioloop.TimeoutError:
-            exc_stack = traceback.format_exc()
+        exc_stack = traceback.format_exc()
         # The stack trace should blame the add_timeout line, not just
         # unrelated IOLoop/testing internals.
         self.assertIn(
index fa965fbe1079db0c43536b3cf71256647e05aac7..fab94e7dc9023ccdcb3b6deef359db78841f7036 100644 (file)
@@ -26,12 +26,9 @@ class RaiseExcInfoTest(unittest.TestCase):
             raise TwoArgException(1, 2)
         except TwoArgException:
             exc_info = sys.exc_info()
-        try:
+        with self.assertRaises(TwoArgException) as context:
             raise_exc_info(exc_info)
-            self.fail("didn't get expected exception")
-        except TwoArgException as e:
-            got_exception = e
-        self.assertIs(got_exception, exc_info[1])
+        self.assertIs(context.exception, exc_info[1])
 
 
 class TestConfigurable(Configurable):