avocado: Add fail_on_error decorator

Give to test writers the fail_on_error decorator, which
should turn any generic exception into an avocado
TestFail.

This is important for some tests that can raise
any exception during a complex operation, and any of those
exceptions can be considered a test failure.
Signed-off-by: NLucas Meneghel Rodrigues <lmr@redhat.com>
上级 b32ffadb
...@@ -13,9 +13,10 @@ ...@@ -13,9 +13,10 @@
# Author: Lucas Meneghel Rodrigues <lmr@redhat.com> # Author: Lucas Meneghel Rodrigues <lmr@redhat.com>
__all__ = ['main', 'Test', 'VERSION'] __all__ = ['main', 'Test', 'VERSION', 'fail_on_error']
from avocado.core.job import main from avocado.core.job import main
from avocado.core.test import Test from avocado.core.test import Test
from avocado.core.version import VERSION from avocado.core.version import VERSION
from avocado.core.exceptions import fail_on_error
...@@ -17,6 +17,29 @@ Exception classes, useful for tests, and other parts of the framework code. ...@@ -17,6 +17,29 @@ Exception classes, useful for tests, and other parts of the framework code.
""" """
def fail_on_error(fn):
"""
Apply to any test you want to FAIL upon any exception raised.
Normally only TestFail called explicitly will mark an avocado test with the
FAIL state, but this decorator is provided as a convenience for people
that need a more relaxed behavior.
:param fn: Function that will be decorated
"""
def new_fn(*args, **kwargs):
try:
return fn(*args, **kwargs)
except TestBaseException:
raise
except Exception, e:
raise TestFail(str(e))
new_fn.__name__ = fn.__name__
new_fn.__doc__ = fn.__doc__
new_fn.__dict__.update(fn.__dict__)
return new_fn
class JobBaseException(Exception): class JobBaseException(Exception):
""" """
......
#!/usr/bin/python
import avocado
class FailOnError(avocado.Test):
"""
Test illustrating the behavior of the fail_on_error decorator.
"""
@avocado.fail_on_error
def test(self):
"""
This should end with FAIL.
Avocado tests should end with ERROR when a generic exception such as
ValueError is raised. The avocado.fail_on_error decorator allows you
to override this behavior, and turn your generic exceptions into
errors.
"""
raise ValueError('This raises a ValueError and should end as a FAIL')
if __name__ == "__main__":
avocado.main()
...@@ -126,6 +126,17 @@ class RunnerOperationTest(unittest.TestCase): ...@@ -126,6 +126,17 @@ class RunnerOperationTest(unittest.TestCase):
result)) result))
self.assertIn('"status": "ERROR"', result.stdout) self.assertIn('"status": "ERROR"', result.stdout)
def test_fail_on_error(self):
os.chdir(basedir)
cmd_line = ("./scripts/avocado run --sysinfo=off --job-results-dir %s "
"--json - fail_on_error" % self.tmpdir)
result = process.run(cmd_line, ignore_status=True)
expected_rc = 1
self.assertEqual(result.exit_status, expected_rc,
"Avocado did not return rc %d:\n%s" % (expected_rc,
result))
self.assertIn('"status": "FAIL"', result.stdout)
def test_runner_timeout(self): def test_runner_timeout(self):
os.chdir(basedir) os.chdir(basedir)
cmd_line = './scripts/avocado run --sysinfo=off --job-results-dir %s --xunit - timeouttest' % self.tmpdir cmd_line = './scripts/avocado run --sysinfo=off --job-results-dir %s --xunit - timeouttest' % self.tmpdir
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册