diff --git a/avocado/core/test.py b/avocado/core/test.py index 77a836c986393e8afaf20d9ef4da5509337b15ab..2797333e9679010927fc6e46b4118eb397007862 100644 --- a/avocado/core/test.py +++ b/avocado/core/test.py @@ -359,8 +359,9 @@ class Test(unittest.TestCase): except exceptions.TestNAError, details: stacktrace.log_exc_info(sys.exc_info(), logger='avocado.test') raise exceptions.TestNAError(details) - except Exception, details: + except: # Old-style exceptions are not inherited from Exception() stacktrace.log_exc_info(sys.exc_info(), logger='avocado.test') + details = sys.exc_info()[1] raise exceptions.TestSetupFail(details) try: testMethod() @@ -371,8 +372,11 @@ class Test(unittest.TestCase): 'must fix your test. Original skip exception: ' '%s' % details) raise exceptions.TestError(skip_illegal_msg) - except Exception, details: + except: # Old-style exceptions are not inherited from Exception() stacktrace.log_exc_info(sys.exc_info(), logger='avocado.test') + details = sys.exc_info()[1] + if not isinstance(details, Exception): # Avoid passing nasty exc + details = exceptions.TestError("%r: %s" % (details, details)) test_exception = details finally: try: @@ -384,9 +388,10 @@ class Test(unittest.TestCase): 'you must fix your test. Original skip ' 'exception: %s' % details) raise exceptions.TestError(skip_illegal_msg) - except Exception, details: + except: # avoid old-style exception failures stacktrace.log_exc_info(sys.exc_info(), logger='avocado.test') - cleanup_exception = details + details = sys.exc_info()[1] + cleanup_exception = exceptions.TestSetupFail(details) whiteboard_file = os.path.join(self.logdir, 'whiteboard') genio.write_file(whiteboard_file, self.whiteboard) @@ -423,7 +428,7 @@ class Test(unittest.TestCase): if test_exception is not None: raise test_exception elif cleanup_exception is not None: - raise exceptions.TestSetupFail(cleanup_exception) + raise cleanup_exception elif stdout_check_exception is not None: raise stdout_check_exception elif stderr_check_exception is not None: diff --git a/examples/tests/failtest_nasty3.py b/examples/tests/failtest_nasty3.py new file mode 100755 index 0000000000000000000000000000000000000000..b698f923b6cad4044b0d2a01b258d4f27809c36a --- /dev/null +++ b/examples/tests/failtest_nasty3.py @@ -0,0 +1,32 @@ +#!/usr/bin/python + +from avocado import Test +from avocado import main + + +class NastyException: + + """ Please never use something like this!!! (old-style exception) """ + + def __init__(self, msg): + self.msg = msg + + def __str__(self): + return self.msg + + +class FailTest(Test): + + """ + This test raises old-style-class exception + """ + + def test(self): + """ + Should fail not-that-badly + """ + raise NastyException("Nasty-string-like-exception") + + +if __name__ == "__main__": + main() diff --git a/selftests/functional/test_standalone.py b/selftests/functional/test_standalone.py index e8f66abc59a12ce045b7968e8a499bcb69c5ae68..d1363400d85911f099c514b5e61752cd295009ce 100644 --- a/selftests/functional/test_standalone.py +++ b/selftests/functional/test_standalone.py @@ -62,6 +62,13 @@ class StandaloneTests(unittest.TestCase): self.assertIn("Exception: Unable to get exception, check the traceback" " for details.", result.stdout) + def test_failtest_nasty3(self): + cmd_line = './examples/tests/failtest_nasty3.py -r' + expected_rc = 1 + result = self.run_and_check(cmd_line, expected_rc, 'failtest_nasty3') + self.assertIn("TestError: