[Python-checkins] CVS: python/dist/src/Lib unittest.py,1.9,1.10

Steve Purcell purcell@users.sourceforge.net
Thu, 06 Sep 2001 01:24:42 -0700


Update of /cvsroot/python/python/dist/src/Lib
In directory usw-pr-cvs1:/tmp/cvs-serv17225

Modified Files:
	unittest.py 
Log Message:
Changed TestResult to store only the text representation of an error.

This patch is similar to that proposed by Jeremy. The proposed patch altered
the interface of TestResult such that it would be passed the error
information as a string rather than an exc_info() tuple.

The implemented change leaves the interface untouched so that TestResults
are still passed the tracebacks, but stor them in stringified form for
later reporting.

Notes:
- Custom subclasses of TestResult written by users should be unaffected.
- The existing 'unittestgui.py' will still work with this module after the
  change.
- Support can later be added to pop into the debugger when an error occurs;
  this support should be added to a TestRunner rather than to TestCase itself,
  which this change will enable.

(Jeremy, Fred, Guido: Thanks for all the feedback)



Index: unittest.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/unittest.py,v
retrieving revision 1.9
retrieving revision 1.10
diff -C2 -d -r1.9 -r1.10
*** unittest.py	2001/08/08 07:57:26	1.9
--- unittest.py	2001/09/06 08:24:40	1.10
***************
*** 17,21 ****
              self.assertEquals((1 + 2), 3)
              self.assertEquals(0 + 1, 1)
!         def testMultiply(self);
              self.assertEquals((0 * 10), 0)
              self.assertEquals((5 * 8), 40)
--- 17,21 ----
              self.assertEquals((1 + 2), 3)
              self.assertEquals(0 + 1, 1)
!         def testMultiply(self):
              self.assertEquals((0 * 10), 0)
              self.assertEquals((5 * 8), 40)
***************
*** 68,73 ****
      Each instance holds the total number of tests run, and collections of
      failures and errors that occurred among those test runs. The collections
!     contain tuples of (testcase, exceptioninfo), where exceptioninfo is a
!     tuple of values as returned by sys.exc_info().
      """
      def __init__(self):
--- 68,73 ----
      Each instance holds the total number of tests run, and collections of
      failures and errors that occurred among those test runs. The collections
!     contain tuples of (testcase, exceptioninfo), where exceptioninfo is the
!     formatted traceback of the error that occurred
      """
      def __init__(self):
***************
*** 86,95 ****
  
      def addError(self, test, err):
!         "Called when an error has occurred"
!         self.errors.append((test, err))
  
      def addFailure(self, test, err):
!         "Called when a failure has occurred"
!         self.failures.append((test, err))
  
      def addSuccess(self, test):
--- 86,98 ----
  
      def addError(self, test, err):
!         """Called when an error has occurred. 'err' is a tuple of values as
!         returned by sys.exc_info().
!         """
!         self.errors.append((test, self._exc_info_to_string(err)))
  
      def addFailure(self, test, err):
!         """Called when an error has occurred. 'err' is a tuple of values as
!         returned by sys.exc_info()."""
!         self.failures.append((test, self._exc_info_to_string(err)))
  
      def addSuccess(self, test):
***************
*** 105,108 ****
--- 108,115 ----
          self.shouldStop = 1
  
+     def _exc_info_to_string(self, err):
+         """Converts a sys.exc_info()-style tuple of values into a string."""
+         return string.join(apply(traceback.format_exception, err), '')
+ 
      def __repr__(self):
          return "<%s run=%i errors=%i failures=%i>" % \
***************
*** 576,582 ****
              self.stream.writeln("%s: %s" % (flavour,self.getDescription(test)))
              self.stream.writeln(self.separator2)
!             for line in apply(traceback.format_exception, err):
!                 for l in string.split(line,"\n")[:-1]:
!                     self.stream.writeln("%s" % l)
  
  
--- 583,587 ----
              self.stream.writeln("%s: %s" % (flavour,self.getDescription(test)))
              self.stream.writeln(self.separator2)
!             self.stream.writeln("%s" % err)