提交 a281534d 编写于 作者: C Cleber Rosa

Test: use more appropriate exceptions on invalid input

The AssertionError exception is intended to be used when an internal
condition that must exist, does not exist.  When dealing with values
received from an outer layer, that we can't control, it's better to
use the more appropriate ValueError.

Another reason is that code such as:

    def run_avocado(self):
        self._setup_environment_variables()
        try:
            self._tag_start()
            self._run_avocado()
    ...
        except AssertionError as detail:
             self.__status = 'FAIL'
             self.__fail_class = detail.__class__.__name__
             self.__fail_reason = str(detail)
             self.__traceback = stacktrace.prepare_exc_info(sys.exc_info())
    ...

Can be afected by the AssertionErrors, and return a test failure
instead of a test error.

Reference: https://docs.python.org/3/library/exceptions.html#ValueError
Reference: https://docs.python.org/3/library/exceptions.html#AssertionErrorSigned-off-by: NCleber Rosa <crosa@redhat.com>
上级 1b6428bb
......@@ -153,7 +153,8 @@ class TestID(object):
For Test ID "001-mytest;foo", examples of shortened file
system versions include "001-mytest;f" or "001-myte;foo".
:raises: AssertionError
:raises: RuntimeError if the test ID cannot be converted to a
filesystem representation.
"""
test_id = str(self)
test_id_fs = astring.string_to_safe_path(test_id)
......@@ -166,9 +167,9 @@ class TestID(object):
elif len(self.str_uid) <= len(test_id_fs): # full uid
return astring.string_to_safe_path(self.str_uid + self.str_variant)
else: # not even uid could be stored in fs
raise AssertionError('Test uid is too long to be stored on the '
'filesystem: "%s"\nFull Test ID: "%s"'
% (self.str_uid, str(self)))
raise RuntimeError('Test ID is too long to be stored on the '
'filesystem: "%s"\nFull Test ID: "%s"'
% (self.str_uid, str(self)))
class TestData(object):
......@@ -570,9 +571,10 @@ class Test(unittest.TestCase, TestData):
"""
Override the runner_queue
"""
self.assertTrue(self.__runner_queue is None, "Overriding of runner_"
"queue multiple times is not allowed -> old=%s new=%s"
% (self.__runner_queue, runner_queue))
if self.__runner_queue is not None:
raise RuntimeError("Overriding of runner_queue multiple "
"times is not allowed -> old=%s new=%s"
% (self.__runner_queue, runner_queue))
self.__runner_queue = runner_queue
@property
......@@ -1206,7 +1208,8 @@ class ExternalRunnerTest(SimpleTest):
external_runner=None, external_runner_argument=None):
if external_runner_argument is None:
external_runner_argument = name.name
self.assertIsNotNone(external_runner, "External runner test requires "
if external_runner is None:
raise ValueError("External runner test requires a valid "
"external_runner parameter, got None instead.")
self.external_runner = external_runner
super(ExternalRunnerTest, self).__init__(name, params, base_logdir,
......
......@@ -90,7 +90,7 @@ class TestClassTestUnit(unittest.TestCase):
tst = check("a" * 255, "whatever", {"variant_id": "whatever-else"},
"a" * 255)
# Impossible to store (uid does not fit
self.assertRaises(AssertionError, check, "a" * 256, "whatever",
self.assertRaises(RuntimeError, check, "a" * 256, "whatever",
{"variant_id": "else"}, None)
self.assertEqual(os.path.basename(tst.workdir),
......@@ -347,7 +347,7 @@ class TestID(unittest.TestCase):
raised.
"""
test_id = test.TestID(1, 'test', no_digits=256)
self.assertRaises(AssertionError, lambda: test_id.str_filesystem)
self.assertRaises(RuntimeError, lambda: test_id.str_filesystem)
def test_uid_large_name(self):
"""
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册