From: Akihiro Yamazaki Date: Sun, 30 Mar 2014 14:15:46 +0000 (+0900) Subject: correct testcases X-Git-Tag: v4.0.0b1~98^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=32c7a328ed2c00d0ec9adc62b036e1bee65ad146;p=thirdparty%2Ftornado.git correct testcases --- diff --git a/tornado/test/concurrent_test.py b/tornado/test/concurrent_test.py index edd70c9a7..5e93ad6a4 100644 --- a/tornado/test/concurrent_test.py +++ b/tornado/test/concurrent_test.py @@ -163,10 +163,12 @@ class ReturnFutureTest(AsyncTestCase): self.expected_frame = traceback.extract_tb( sys.exc_info()[2], limit=1)[0] raise - with self.assertRaises(ZeroDivisionError): + try: yield f() - tb = traceback.extract_tb(sys.exc_info()[2]) - self.assertIn(self.expected_frame, tb) + self.fail("didn't get expected exception") + except ZeroDivisionError: + tb = traceback.extract_tb(sys.exc_info()[2]) + self.assertIn(self.expected_frame, tb) # The following series of classes demonstrate and test various styles # of use, with and without generators and futures. diff --git a/tornado/test/ioloop_test.py b/tornado/test/ioloop_test.py index b7d1f3a69..ff26bde1e 100644 --- a/tornado/test/ioloop_test.py +++ b/tornado/test/ioloop_test.py @@ -112,9 +112,12 @@ class TestIOLoop(AsyncTestCase): thread = threading.Thread(target=target) thread.start() closing.wait() - with self.assertRaisesRegexp(RuntimeError, "\AIOLoop is closing\Z"): - for i in range(1000): + for i in range(1000): + try: other_ioloop.add_callback(lambda: None) + except RuntimeError as e: + self.assertEqual("IOLoop is closing", str(e)) + break 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 181008dc5..32bbe4217 100644 --- a/tornado/test/template_test.py +++ b/tornado/test/template_test.py @@ -149,14 +149,20 @@ try{% set y = 1/x %} self.assertEqual(result, b"013456") def test_break_outside_loop(self): - with self.assertRaises(ParseError): + try: 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 - with self.assertRaises(ParseError): + try: 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') @@ -174,50 +180,58 @@ class StackTraceTest(unittest.TestCase): two{{1/0}} three """}) - with self.assertRaises(ZeroDivisionError): + try: loader.load("test.html").generate() - exc_stack = traceback.format_exc() - self.assertTrue("# test.html:2" in exc_stack) + self.fail("did not get expected exception") + except ZeroDivisionError: + self.assertTrue("# test.html:2" in traceback.format_exc()) def test_error_line_number_directive(self): loader = DictLoader({"test.html": """one two{%if 1/0%} three{%end%} """}) - with self.assertRaises(ZeroDivisionError): + try: loader.load("test.html").generate() - exc_stack = traceback.format_exc() - self.assertTrue("# test.html:2" in exc_stack) + self.fail("did not get expected exception") + except ZeroDivisionError: + self.assertTrue("# test.html:2" in traceback.format_exc()) def test_error_line_number_module(self): loader = DictLoader({ "base.html": "{% module Template('sub.html') %}", "sub.html": "{{1/0}}", }, namespace={"_tt_modules": ObjectDict({"Template": lambda path, **kwargs: loader.load(path).generate(**kwargs)})}) - with self.assertRaises(ZeroDivisionError): + try: loader.load("base.html").generate() - exc_stack = traceback.format_exc() - self.assertTrue('# base.html:1' in exc_stack) - self.assertTrue('# sub.html:1' in exc_stack) + self.fail("did not get expected exception") + except ZeroDivisionError: + exc_stack = traceback.format_exc() + self.assertTrue('# base.html:1' in exc_stack) + self.assertTrue('# sub.html:1' in exc_stack) def test_error_line_number_include(self): loader = DictLoader({ "base.html": "{% include 'sub.html' %}", "sub.html": "{{1/0}}", }) - with self.assertRaises(ZeroDivisionError): + try: loader.load("base.html").generate() - exc_stack = traceback.format_exc() - self.assertTrue("# sub.html:1 (via base.html:1)" in exc_stack) + self.fail("did not get expected exception") + except ZeroDivisionError: + self.assertTrue("# sub.html:1 (via base.html:1)" in + traceback.format_exc()) def test_error_line_number_extends_base_error(self): loader = DictLoader({ "base.html": "{{1/0}}", "sub.html": "{% extends 'base.html' %}", }) - with self.assertRaises(ZeroDivisionError): + try: loader.load("sub.html").generate() - exc_stack = traceback.format_exc() + self.fail("did not get expected exception") + except ZeroDivisionError: + exc_stack = traceback.format_exc() self.assertTrue("# base.html:1" in exc_stack) def test_error_line_number_extends_sub_error(self): @@ -229,10 +243,12 @@ three{%end%} {{1/0}} {% end %} """}) - with self.assertRaises(ZeroDivisionError): + try: loader.load("sub.html").generate() - exc_stack = traceback.format_exc() - self.assertTrue("# sub.html:4 (via base.html:1)" in exc_stack) + self.fail("did not get expected exception") + except ZeroDivisionError: + self.assertTrue("# sub.html:4 (via base.html:1)" in + traceback.format_exc()) def test_multi_includes(self): loader = DictLoader({ @@ -240,10 +256,12 @@ three{%end%} "b.html": "{% include 'c.html' %}", "c.html": "{{1/0}}", }) - with self.assertRaises(ZeroDivisionError): + try: loader.load("a.html").generate() - exc_stack = traceback.format_exc() - self.assertTrue("# c.html:1 (via b.html:1, a.html:1)" in exc_stack) + self.fail("did not get expected exception") + except ZeroDivisionError: + self.assertTrue("# c.html:1 (via b.html:1, a.html:1)" in + traceback.format_exc()) class AutoEscapeTest(unittest.TestCase): diff --git a/tornado/test/testing_test.py b/tornado/test/testing_test.py index 92eb9a48d..aabdaced7 100644 --- a/tornado/test/testing_test.py +++ b/tornado/test/testing_test.py @@ -28,8 +28,11 @@ def set_environ(name, value): class AsyncTestCaseTest(AsyncTestCase): def test_exception_in_callback(self): self.io_loop.add_callback(lambda: 1 / 0) - with self.assertRaises(ZeroDivisionError): + try: self.wait() + self.fail("did not get expected exception") + except ZeroDivisionError: + pass def test_wait_timeout(self): time = self.io_loop.time @@ -148,14 +151,15 @@ class GenTest(AsyncTestCase): # This can't use assertRaises because we need to inspect the # exc_info triple (and not just the exception object) - with self.assertRaises(ioloop.TimeoutError): + try: test(self) - exc_stack = traceback.format_exc() - # The stack trace should blame the add_timeout line, not just - # unrelated IOLoop/testing internals. - self.assertIn( - "gen.Task(self.io_loop.add_timeout, self.io_loop.time() + 1)", - exc_stack) + self.fail("did not get expected exception") + except ioloop.TimeoutError: + # The stack trace should blame the add_timeout line, not just + # unrelated IOLoop/testing internals. + self.assertIn( + "gen.Task(self.io_loop.add_timeout, self.io_loop.time() + 1)", + traceback.format_exc()) self.finished = True diff --git a/tornado/test/util_test.py b/tornado/test/util_test.py index fab94e7dc..41ccbb9a5 100644 --- a/tornado/test/util_test.py +++ b/tornado/test/util_test.py @@ -26,9 +26,11 @@ class RaiseExcInfoTest(unittest.TestCase): raise TwoArgException(1, 2) except TwoArgException: exc_info = sys.exc_info() - with self.assertRaises(TwoArgException) as context: + try: raise_exc_info(exc_info) - self.assertIs(context.exception, exc_info[1]) + self.fail("didn't get expected exception") + except TwoArgException as e: + self.assertIs(e, exc_info[1]) class TestConfigurable(Configurable):