From: Akihiro Yamazaki Date: Thu, 27 Mar 2014 15:27:39 +0000 (+0900) Subject: use "with self.assertRaises" pattern X-Git-Tag: v4.0.0b1~98^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e914a5143e8fd99c4904eedc7547d0d637d752de;p=thirdparty%2Ftornado.git use "with self.assertRaises" pattern --- diff --git a/tornado/test/concurrent_test.py b/tornado/test/concurrent_test.py index 2506ef45e..edd70c9a7 100644 --- a/tornado/test/concurrent_test.py +++ b/tornado/test/concurrent_test.py @@ -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 diff --git a/tornado/test/httpclient_test.py b/tornado/test/httpclient_test.py index 984f5e0c5..78daa74dc 100644 --- a/tornado/test/httpclient_test.py +++ b/tornado/test/httpclient_test.py @@ -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): diff --git a/tornado/test/ioloop_test.py b/tornado/test/ioloop_test.py index bd069c249..b7d1f3a69 100644 --- a/tornado/test/ioloop_test.py +++ b/tornado/test/ioloop_test.py @@ -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 diff --git a/tornado/test/template_test.py b/tornado/test/template_test.py index c1b1e48ec..181008dc5 100644 --- a/tornado/test/template_test.py +++ b/tornado/test/template_test.py @@ -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) diff --git a/tornado/test/testing_test.py b/tornado/test/testing_test.py index ef545e762..92eb9a48d 100644 --- a/tornado/test/testing_test.py +++ b/tornado/test/testing_test.py @@ -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( diff --git a/tornado/test/util_test.py b/tornado/test/util_test.py index fa965fbe1..fab94e7dc 100644 --- a/tornado/test/util_test.py +++ b/tornado/test/util_test.py @@ -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):