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

Steve Purcell purcell@users.sourceforge.net
Thu, 12 Apr 2001 02:05:03 -0700


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

Modified Files:
	unittest.py 
Log Message:
- New fail*() methods, and comprehensive set of assert*() synonyms
- TestCase.failureException defines the exception that indicates a test failure
- Docstrings for TestLoader class
- Added exc_info() hack back in


Index: unittest.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/unittest.py,v
retrieving revision 1.4
retrieving revision 1.5
diff -C2 -r1.4 -r1.5
*** unittest.py	2001/04/09 15:37:31	1.4
--- unittest.py	2001/04/12 09:05:01	1.5
***************
*** 132,135 ****
--- 132,142 ----
      in order to be run.
      """
+ 
+     # This attribute determines which exception will be raised when
+     # the instance's assertion methods fail; test methods raising this
+     # exception will be deemed to have 'failed' rather than 'errored'
+ 
+     failureException = AssertionError
+ 
      def __init__(self, methodName='runTest'):
          """Create an instance of the class that will use the named test
***************
*** 190,194 ****
                  self.setUp()
              except:
!                 result.addError(self,sys.exc_info())
                  return
  
--- 197,201 ----
                  self.setUp()
              except:
!                 result.addError(self,self.__exc_info())
                  return
  
***************
*** 197,209 ****
                  testMethod()
                  ok = 1
!             except AssertionError, e:
!                 result.addFailure(self,sys.exc_info())
              except:
!                 result.addError(self,sys.exc_info())
  
              try:
                  self.tearDown()
              except:
!                 result.addError(self,sys.exc_info())
                  ok = 0
              if ok: result.addSuccess(self)
--- 204,216 ----
                  testMethod()
                  ok = 1
!             except self.failureException, e:
!                 result.addFailure(self,self.__exc_info())
              except:
!                 result.addError(self,self.__exc_info())
  
              try:
                  self.tearDown()
              except:
!                 result.addError(self,self.__exc_info())
                  ok = 0
              if ok: result.addSuccess(self)
***************
*** 217,235 ****
          self.tearDown()
  
!     def assert_(self, expr, msg=None):
!         """Equivalent of built-in 'assert', but is not optimised out when
!            __debug__ is false.
          """
!         if not expr:
!             raise AssertionError, msg
  
!     failUnless = assert_
  
      def failIf(self, expr, msg=None):
          "Fail the test if the expression is true."
!         apply(self.assert_,(not expr,msg))
  
!     def assertRaises(self, excClass, callableObj, *args, **kwargs):
!         """Assert that an exception of class excClass is thrown
             by callableObj when invoked with arguments args and keyword
             arguments kwargs. If a different type of exception is
--- 224,254 ----
          self.tearDown()
  
!     def __exc_info(self):
!         """Return a version of sys.exc_info() with the traceback frame
!            minimised; usually the top level of the traceback frame is not
!            needed.
          """
!         exctype, excvalue, tb = sys.exc_info()
!         if sys.platform[:4] == 'java': ## tracebacks look different in Jython
!             return (exctype, excvalue, tb)
!         newtb = tb.tb_next
!         if newtb is None:
!             return (exctype, excvalue, tb)
!         return (exctype, excvalue, newtb)
  
!     def fail(self, msg=None):
!         """Fail immediately, with the given message."""
!         raise self.failureException, msg
  
      def failIf(self, expr, msg=None):
          "Fail the test if the expression is true."
!         if expr: raise self.failureException, msg
! 
!     def failUnless(self, expr, msg=None):
!         """Fail the test unless the expression is true."""
!         if not expr: raise self.failureException, msg
  
!     def failUnlessRaises(self, excClass, callableObj, *args, **kwargs):
!         """Fail unless an exception of class excClass is thrown
             by callableObj when invoked with arguments args and keyword
             arguments kwargs. If a different type of exception is
***************
*** 245,271 ****
              if hasattr(excClass,'__name__'): excName = excClass.__name__
              else: excName = str(excClass)
!             raise AssertionError, excName
  
!     def assertEquals(self, first, second, msg=None):
!         """Assert that the two objects are equal as determined by the '=='
             operator.
          """
!         self.assert_((first == second), msg or '%s != %s' % (first, second))
  
!     def assertNotEquals(self, first, second, msg=None):
!         """Assert that the two objects are unequal as determined by the '!='
             operator.
          """
!         self.assert_((first != second), msg or '%s == %s' % (first, second))
  
!     assertEqual = assertEquals
  
!     assertNotEqual = assertNotEquals
  
!     def fail(self, msg=None):
!         """Fail immediately, with the given message."""
!         raise AssertionError, msg
  
  
  class TestSuite:
      """A test suite is a composite test consisting of a number of TestCases.
--- 264,293 ----
              if hasattr(excClass,'__name__'): excName = excClass.__name__
              else: excName = str(excClass)
!             raise self.failureException, excName
  
!     def failUnlessEqual(self, first, second, msg=None):
!         """Fail if the two objects are unequal as determined by the '!='
             operator.
          """
!         if first != second:
!             raise self.failureException, (msg or '%s != %s' % (first, second))
  
!     def failIfEqual(self, first, second, msg=None):
!         """Fail if the two objects are equal as determined by the '=='
             operator.
          """
!         if first == second:
!             raise self.failureException, (msg or '%s != %s' % (first, second))
  
!     assertEqual = assertEquals = failUnlessEqual
  
!     assertNotEqual = assertNotEquals = failIfEqual
  
!     assertRaises = failUnlessRaises
  
+     assert_ = failUnless
  
+ 
+ 
  class TestSuite:
      """A test suite is a composite test consisting of a number of TestCases.
***************
*** 365,370 ****
      """This class is responsible for loading tests according to various
      criteria and returning them wrapped in a Test
- 
-     It can load all tests within a given, module
      """
      testMethodPrefix = 'test'
--- 387,390 ----
***************
*** 373,380 ****
--- 393,402 ----
  
      def loadTestsFromTestCase(self, testCaseClass):
+         """Return a suite of all tests cases contained in testCaseClass"""
          return self.suiteClass(map(testCaseClass,
                                     self.getTestCaseNames(testCaseClass)))
  
      def loadTestsFromModule(self, module):
+         """Return a suite of all tests cases contained in the given module"""
          tests = []
          for name in dir(module):
***************
*** 385,388 ****
--- 407,418 ----
  
      def loadTestsFromName(self, name, module=None):
+         """Return a suite of all tests cases given a string specifier.
+ 
+         The name may resolve either to a module, a test case class, a
+         test method within a test case class, or a callable object which
+         returns a TestCase or TestSuite instance.
+         
+         The method optionally resolves the names relative to a given module.
+         """
          parts = string.split(name, '.')
          if module is None:
***************
*** 420,423 ****
--- 450,456 ----
  
      def loadTestsFromNames(self, names, module=None):
+         """Return a suite of all tests cases found using the given sequence
+         of string specifiers. See 'loadTestsFromName()'.
+         """
          suites = []
          for name in names:
***************
*** 426,429 ****
--- 459,464 ----
  
      def getTestCaseNames(self, testCaseClass):
+         """Return a sorted sequence of method names found within testCaseClass
+         """
          testFnNames = filter(lambda n,p=self.testMethodPrefix: n[:len(p)] == p,
                               dir(testCaseClass))