[4/8] utils.py: Make the TestFailure exception print better message

Submitted by Dylan Baker on Aug. 22, 2015, 12:21 a.m.

Details

Message ID 1440202880-20285-5-git-send-email-baker.dylan.c@gmail.com
State New, archived
Headers show

Not browsing as part of any series.

Commit Message

Dylan Baker Aug. 22, 2015, 12:21 a.m.
This adds a better error message to TestFailure, which is raised by most
of the helpers in the utils module if a Test fails.

Signed-off-by: Dylan Baker <dylanx.c.baker@intel.com>
---
 framework/tests/utils.py | 24 ++++++++++++++++++++++--
 1 file changed, 22 insertions(+), 2 deletions(-)

Patch hide | download patch | download mbox

diff --git a/framework/tests/utils.py b/framework/tests/utils.py
index 4829620..64aa0f5 100644
--- a/framework/tests/utils.py
+++ b/framework/tests/utils.py
@@ -85,7 +85,27 @@  _SAVED_COMPRESSION = os.environ.get('PIGLIT_COMPRESSION')
 
 
 class TestFailure(AssertionError):
-    pass
+    """An exception to be raised when a test fails.
+
+    Nose expects an AssertionError for test failures, so this is a sublcass of
+    AssertionError.
+
+    It provides the benefit of being able to take either a text message to
+    print, or an exception instance. When passed text it will print the message
+    exactly, when passed an exception it will print the exception type and the
+    str() value of that exception.
+
+    """
+    def __init__(self, arg):
+        super(TestFailure, self).__init__(self)
+        self.__arg = arg
+
+    def __str__(self):
+        if isinstance(self.__arg, Exception):
+            return 'exception type "{}" with message "{}" raised.'.format(
+                repr(self.__arg), str(self.__arg))
+        else:
+            return self.__arg
 
 
 class UtilsError(Exception):
@@ -348,7 +368,7 @@  def not_raises(exceptions):
             try:
                 func(*args, **kwargs)
             except exceptions as e:
-                raise TestFailure(str(e))
+                raise TestFailure(e)
 
         return _inner