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

Dylan Baker baker.dylan.c at gmail.com
Fri Aug 21 17:21:16 PDT 2015


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 at intel.com>
---
 framework/tests/utils.py | 24 ++++++++++++++++++++++--
 1 file changed, 22 insertions(+), 2 deletions(-)

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
 
-- 
2.5.0



More information about the Piglit mailing list