From c3bc80cf9f9c107d9ee2e4205636c091bc9cb430 Mon Sep 17 00:00:00 2001 From: JUNJIE NAN Date: Mon, 17 Feb 2014 13:44:57 +0800 Subject: [PATCH] Replace try...except with assertRaises Even the exception was not raised, try...except... still can pass the test cases. So change to assertRaises. Change-Id: Idec096d66fe8019cfa009dfa12e36bd4827485ad --- heatclient/tests/test_common_http.py | 19 ++++++---------- heatclient/tests/test_shell.py | 34 +++++++++------------------- 2 files changed, 18 insertions(+), 35 deletions(-) diff --git a/heatclient/tests/test_common_http.py b/heatclient/tests/test_common_http.py index 3388c94d..d1185dc9 100644 --- a/heatclient/tests/test_common_http.py +++ b/heatclient/tests/test_common_http.py @@ -386,12 +386,9 @@ class HttpClientTest(testtools.TestCase): # Replay, create client, assert self.m.ReplayAll() client = http.HTTPClient('http://example.com:8004') - try: - client.json_request('GET', '') - self.fail('No exception raised') - except exc.HTTPNotFound as e: - # Assert that the raised exception can be converted to string - self.assertIsNotNone(e.message) + e = self.assertRaises(exc.HTTPNotFound, client.json_request, 'GET', '') + # Assert that the raised exception can be converted to string + self.assertIsNotNone(str(e)) self.m.VerifyAll() def test_http_300_json_request(self): @@ -409,12 +406,10 @@ class HttpClientTest(testtools.TestCase): # Replay, create client, assert self.m.ReplayAll() client = http.HTTPClient('http://example.com:8004') - try: - client.json_request('GET', '') - self.fail('No exception raised') - except exc.HTTPMultipleChoices as e: - # Assert that the raised exception can be converted to string - self.assertIsNotNone(e.message) + e = self.assertRaises( + exc.HTTPMultipleChoices, client.json_request, 'GET', '') + # Assert that the raised exception can be converted to string + self.assertIsNotNone(str(e)) self.m.VerifyAll() def test_fake_json_request(self): diff --git a/heatclient/tests/test_shell.py b/heatclient/tests/test_shell.py index 65e5c073..33923d43 100644 --- a/heatclient/tests/test_shell.py +++ b/heatclient/tests/test_shell.py @@ -417,10 +417,8 @@ class ShellTestUserPass(ShellBase): self.m.ReplayAll() - try: - self.shell("stack-show bad") - except exc.HTTPException as e: - self.assertEqual("ERROR: " + message, str(e)) + e = self.assertRaises(exc.HTTPException, self.shell, "stack-show bad") + self.assertEqual("ERROR: " + message, str(e)) def test_parsable_verbose(self): message = "The Stack (bad) could not be found." @@ -442,22 +440,16 @@ class ShellTestUserPass(ShellBase): exc.verbose = 1 - try: - self.shell("stack-show bad") - except exc.HTTPException as e: - expect = 'ERROR: The Stack (bad) could not be found.\n' - self.assertEqual(expect, str(e)) + e = self.assertRaises(exc.HTTPException, self.shell, "stack-show bad") + self.assertIn(message, str(e)) def test_parsable_malformed_error(self): invalid_json = "ERROR: {Invalid JSON Error." self._script_keystone_client() fakes.script_heat_error(invalid_json) self.m.ReplayAll() - - try: - self.shell("stack-show bad") - except exc.HTTPException as e: - self.assertEqual("ERROR: " + invalid_json, str(e)) + e = self.assertRaises(exc.HTTPException, self.shell, "stack-show bad") + self.assertEqual("ERROR: " + invalid_json, str(e)) def test_parsable_malformed_error_missing_message(self): missing_message = { @@ -474,10 +466,8 @@ class ShellTestUserPass(ShellBase): fakes.script_heat_error(jsonutils.dumps(missing_message)) self.m.ReplayAll() - try: - self.shell("stack-show bad") - except exc.HTTPException as e: - self.assertEqual("ERROR: Internal Error", str(e)) + e = self.assertRaises(exc.HTTPException, self.shell, "stack-show bad") + self.assertEqual("ERROR: Internal Error", str(e)) def test_parsable_malformed_error_missing_traceback(self): message = "The Stack (bad) could not be found." @@ -497,11 +487,9 @@ class ShellTestUserPass(ShellBase): exc.verbose = 1 - try: - self.shell("stack-show bad") - except exc.HTTPException as e: - self.assertEqual("ERROR: The Stack (bad) could not be found.\n", - str(e)) + e = self.assertRaises(exc.HTTPException, self.shell, "stack-show bad") + self.assertEqual("ERROR: The Stack (bad) could not be found.\n", + str(e)) def test_stack_show(self): self._script_keystone_client()