diff --git a/avocado/test.py b/avocado/test.py index b5dd8cfe26225467607937d48db22c4d5af8a32b..ea73462651192e10c5f0458a3543f50025e458c5 100644 --- a/avocado/test.py +++ b/avocado/test.py @@ -270,17 +270,30 @@ class Test(unittest.TestCase): sysinfo_logger = sysinfo.SysInfo(basedir=self.sysinfodir) self.start_logging() sysinfo_logger.start_job_hook() + action_exception = None + cleanup_exception = None try: self.setup() except Exception, details: log_exc_info(sys.exc_info()) raise exceptions.TestSetupFail(details) - self.action() try: - self.cleanup() + self.action() except Exception, details: log_exc_info(sys.exc_info()) - raise exceptions.TestSetupFail(details) + action_exception = details + finally: + try: + self.cleanup() + except Exception, details: + log_exc_info(sys.exc_info()) + cleanup_exception = details + # pylint: disable=E0702 + if action_exception is not None: + raise action_exception + elif cleanup_exception is not None: + raise exceptions.TestSetupFail(cleanup_exception) + self.status = 'PASS' def run_avocado(self, result=None): diff --git a/selftests/all/functional/avocado/basic_tests.py b/selftests/all/functional/avocado/basic_tests.py index f64ee0254f59e6f28efd6f8999138a3a50a0aa8e..b4eb4972f025b180635a02fafa23c38c8307dbb3 100644 --- a/selftests/all/functional/avocado/basic_tests.py +++ b/selftests/all/functional/avocado/basic_tests.py @@ -64,6 +64,23 @@ class RunnerOperationTest(unittest.TestCase): self.assertEqual(result.exit_status, expected_rc, "Avocado did not return rc %d:\n%s" % (expected_rc, result)) + def test_runner_doublefail(self): + os.chdir(basedir) + cmd_line = './scripts/avocado --xunit run doublefail' + result = process.run(cmd_line, ignore_status=True) + output = result.stdout + expected_rc = 1 + unexpected_rc = 3 + self.assertNotEqual(result.exit_status, unexpected_rc, + "Avocado crashed (rc %d):\n%s" % (unexpected_rc, result)) + self.assertEqual(result.exit_status, expected_rc, + "Avocado did not return rc %d:\n%s" % (expected_rc, result)) + self.assertIn("TestError: Failing during cleanup. Yay!", output, + "Cleanup exception not printed to log output") + self.assertIn("FAIL doublefail.1 -> TestFail: This test is supposed to fail", + output, + "Test did not fail with action exception") + class RunnerDropinTest(unittest.TestCase): diff --git a/tests/doublefail/doublefail.py b/tests/doublefail/doublefail.py new file mode 100755 index 0000000000000000000000000000000000000000..394504ad516fa49a8ca6e62bd21c2dee206f6fa4 --- /dev/null +++ b/tests/doublefail/doublefail.py @@ -0,0 +1,43 @@ +#!/usr/bin/python + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +# +# See LICENSE for more details. +# +# Copyright: Red Hat Inc. 2013-2014 +# Author: Lucas Meneghel Rodrigues + + +from avocado import test +from avocado import job +from avocado.core import exceptions + + +class doublefail(test.Test): + + """ + Functional test for avocado. Straight up fail the test. + """ + + def action(self): + """ + Should fail. + """ + raise exceptions.TestFail('This test is supposed to fail') + + def cleanup(self): + """ + Should also fail. + """ + raise exceptions.TestError('Failing during cleanup. Yay!') + + +if __name__ == "__main__": + job.main()