[Python-checkins] CVS: python/dist/src/Lib/test README,1.9,1.10 regrtest.py,1.47,1.48 test_cookie.py,1.10,1.11 test_descrtut.py,1.4,1.5 test_difflib.py,1.3,1.4 test_doctest.py,1.2,1.3 test_generators.py,1.28,1.29 test_support.py,1.28,1.29

Tim Peters tim_one@users.sourceforge.net
Sat, 08 Sep 2001 23:12:03 -0700


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

Modified Files:
	README regrtest.py test_cookie.py test_descrtut.py 
	test_difflib.py test_doctest.py test_generators.py 
	test_support.py 
Log Message:
Teach regrtest how to pass on doctest failure msgs.  This is done via a
horridly inefficient hack in regrtest's Compare class, but it's about as
clean as can be:  regrtest has to set up the Compare instance before
importing a test module, and by the time the module *is* imported it's too
late to change that decision.  The good news is that the more tests we
convert to unittest and doctest, the less the inefficiency here matters.
Even now there are few tests with large expected-output files (the new
cost here is a Python-level call per .write() when there's an expected-
output file).


Index: README
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/test/README,v
retrieving revision 1.9
retrieving revision 1.10
diff -C2 -d -r1.9 -r1.10
*** README	2001/05/23 13:24:30	1.9
--- README	2001/09/09 06:12:01	1.10
***************
*** 56,67 ****
  provides a convenient example:
  
!     from test_support import verbose
!     import doctest, difflib
!     doctest.testmod(difflib, verbose=verbose)
  
  If the test is successful, nothing is written to stdout (so you should not
  create a corresponding output/test_difflib file), but running regrtest
! with -v will give a detailed report, the same as if passing -v to doctest
! (that's what importing verbose from test_support accomplishes).
  
  See the documentation for the doctest module for information on
--- 56,85 ----
  provides a convenient example:
  
!     import difflib, test_support
!     test_support.run_doctest(difflib)
  
  If the test is successful, nothing is written to stdout (so you should not
  create a corresponding output/test_difflib file), but running regrtest
! with -v will give a detailed report, the same as if passing -v to doctest.
! 
! A second argument can be passed to run_doctest to tell doctest to search
! sys.argv for -v instead of using test_support's idea of verbosity.  This
! is useful for writing doctest-based tests that aren't simply running a
! doctest'ed Lib module, but contain the doctests themselves.  Then at
! times you may want to run such a test directly as a doctest, independent
! of the regrtest framework.  The tail end of test_descrtut.py is a good
! example:
! 
!     def test_main(verbose=None):
!         import test_support, test.test_descrtut
!         test_support.run_doctest(test.test_descrtut, verbose)
! 
!     if __name__ == "__main__":
!         test_main(1)
! 
! If run via regrtest, test_main() is called (by regrtest) without specifying
! verbose, and then test_supprot's idea of verbosity is used.  But when
! run directly, test_main(1) is called, and then doctest's idea of verbosity
! is used.
  
  See the documentation for the doctest module for information on

Index: regrtest.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/test/regrtest.py,v
retrieving revision 1.47
retrieving revision 1.48
diff -C2 -d -r1.47 -r1.48
*** regrtest.py	2001/09/06 16:09:41	1.47
--- regrtest.py	2001/09/09 06:12:01	1.48
***************
*** 289,293 ****
              cfp = sys.stdout
          else:
!             cfp = Compare(outputfile)
      except IOError:
          cfp = None
--- 289,293 ----
              cfp = sys.stdout
          else:
!             cfp = Compare(outputfile, sys.stdout)
      except IOError:
          cfp = None
***************
*** 387,391 ****
  
  class Compare:
!     def __init__(self, filename):
          if os.path.exists(filename):
              self.fp = open(filename, 'r')
--- 387,392 ----
  
  class Compare:
!     def __init__(self, filename, origstdout):
!         self.origstdout = origstdout
          if os.path.exists(filename):
              self.fp = open(filename, 'r')
***************
*** 396,399 ****
--- 397,403 ----
  
      def write(self, data):
+         if test_support.suppress_output_comparison():
+             self.origstdout.write(data)
+             return
          expected = self.fp.read(len(data))
          if data == expected:

Index: test_cookie.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/test/test_cookie.py,v
retrieving revision 1.10
retrieving revision 1.11
diff -C2 -d -r1.10 -r1.11
*** test_cookie.py	2001/05/13 00:19:31	1.10
--- test_cookie.py	2001/09/09 06:12:01	1.11
***************
*** 1,8 ****
  # Simple test suite for Cookie.py
  
! from test_support import verify
  import Cookie
- from test_support import verify, verbose
- import doctest
  
  # Currently this only tests SimpleCookie
--- 1,6 ----
  # Simple test suite for Cookie.py
  
! from test_support import verify, verbose, run_doctest
  import Cookie
  
  # Currently this only tests SimpleCookie
***************
*** 47,49 ****
  
  print "If anything blows up after this line, it's from Cookie's doctest."
! doctest.testmod(Cookie)
--- 45,47 ----
  
  print "If anything blows up after this line, it's from Cookie's doctest."
! run_doctest(Cookie)

Index: test_descrtut.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/test/test_descrtut.py,v
retrieving revision 1.4
retrieving revision 1.5
diff -C2 -d -r1.4 -r1.5
*** test_descrtut.py	2001/09/09 01:21:31	1.4
--- test_descrtut.py	2001/09/09 06:12:01	1.5
***************
*** 485,493 ****
  # Note that doctest and regrtest both look in sys.argv for a "-v" argument,
  # so this works as expected in both ways of running regrtest.
! def test_main():
!     import doctest, test.test_descrtut
!     doctest.testmod(test.test_descrtut)
  
  # This part isn't needed for regrtest, but for running the test directly.
  if __name__ == "__main__":
!     test_main()
--- 485,498 ----
  # Note that doctest and regrtest both look in sys.argv for a "-v" argument,
  # so this works as expected in both ways of running regrtest.
! def test_main(verbose=None):
!     # Obscure:  import this module as test.test_descrtut instead of as
!     # plain test_descrtut because the name of this module works its way
!     # into the doctest examples, and unless the full test.test_descrtut
!     # business is used the name can change depending on how the test is
!     # invoked.
!     import test_support, test.test_descrtut
!     test_support.run_doctest(test.test_descrtut, verbose)
  
  # This part isn't needed for regrtest, but for running the test directly.
  if __name__ == "__main__":
!     test_main(1)

Index: test_difflib.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/test/test_difflib.py,v
retrieving revision 1.3
retrieving revision 1.4
diff -C2 -d -r1.3 -r1.4
*** test_difflib.py	2001/05/23 07:46:36	1.3
--- test_difflib.py	2001/09/09 06:12:01	1.4
***************
*** 1,3 ****
! from test_support import verbose
! import doctest, difflib
! doctest.testmod(difflib, verbose=verbose)
--- 1,2 ----
! import difflib, test_support
! test_support.run_doctest(difflib)

Index: test_doctest.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/test/test_doctest.py,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -d -r1.2 -r1.3
*** test_doctest.py	2001/05/23 07:46:36	1.2
--- test_doctest.py	2001/09/09 06:12:01	1.3
***************
*** 1,3 ****
! from test_support import verbose
! import doctest
! doctest.testmod(doctest, verbose=verbose)
--- 1,2 ----
! import doctest, test_support 
! test_support.run_doctest(doctest)

Index: test_generators.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/test/test_generators.py,v
retrieving revision 1.28
retrieving revision 1.29
diff -C2 -d -r1.28 -r1.29
*** test_generators.py	2001/09/03 05:47:38	1.28
--- test_generators.py	2001/09/09 06:12:01	1.29
***************
*** 1352,1366 ****
  # Note that doctest and regrtest both look in sys.argv for a "-v" argument,
  # so this works as expected in both ways of running regrtest.
! def test_main():
!     import doctest, test_generators
      if 0:   # change to 1 to run forever (to check for leaks)
          while 1:
              doctest.master = None
!             doctest.testmod(test_generators)
              print ".",
      else:
!         doctest.testmod(test_generators)
  
  # This part isn't needed for regrtest, but for running the test directly.
  if __name__ == "__main__":
!     test_main()
--- 1352,1366 ----
  # Note that doctest and regrtest both look in sys.argv for a "-v" argument,
  # so this works as expected in both ways of running regrtest.
! def test_main(verbose=None):
!     import doctest, test_support, test_generators
      if 0:   # change to 1 to run forever (to check for leaks)
          while 1:
              doctest.master = None
!             test_support.run_doctest(test_generators, verbose)
              print ".",
      else:
!         test_support.run_doctest(test_generators, verbose)
  
  # This part isn't needed for regrtest, but for running the test directly.
  if __name__ == "__main__":
!     test_main(1)

Index: test_support.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/test/test_support.py,v
retrieving revision 1.28
retrieving revision 1.29
diff -C2 -d -r1.28 -r1.29
*** test_support.py	2001/09/08 03:37:56	1.28
--- test_support.py	2001/09/09 06:12:01	1.29
***************
*** 3,7 ****
  import sys
  
- 
  class Error(Exception):
      """Base class for regression test exceptions."""
--- 3,6 ----
***************
*** 23,26 ****
--- 22,45 ----
  use_resources = None       # Flag set to [] by regrtest.py
  
+ # _output_comparison controls whether regrtest will try to compare stdout
+ # with an expected-output file.  For straight regrtests, it should.
+ # The doctest driver should set_output_comparison(0) for the duration, and
+ # restore the old value when it's done.
+ # Note that this control is in addition to verbose mode:  output will be
+ # compared if and only if _output_comparison is true and verbose mode is
+ # not in effect.
+ _output_comparison = 1
+ 
+ def set_output_comparison(newvalue):
+     global _output_comparison
+     oldvalue = _output_comparison
+     _output_comparison = newvalue
+     return oldvalue
+ 
+ # regrtest's interface to _output_comparison.
+ def suppress_output_comparison():
+     return not _output_comparison
+ 
+ 
  def unload(name):
      try:
***************
*** 157,158 ****
--- 176,203 ----
                               % (testclass.__module__, testclass.__name__))
          raise TestFailed(err)
+ 
+ #=======================================================================
+ # doctest driver.
+ 
+ def run_doctest(module, verbosity=None):
+     """Run doctest on the given module.
+ 
+     If optional argument verbosity is not specified (or is None), pass
+     test_support's belief about verbosity on to doctest.  Else doctest
+     sys.argv for -v.
+     """
+ 
+     import doctest
+ 
+     if verbosity is None:
+         verbosity = verbose
+     else:
+         verbosity = None
+ 
+     oldvalue = set_output_comparison(0)
+     try:
+         f, t = doctest.testmod(module, verbose=verbosity)
+         if f:
+             raise TestFailed("%d of %d doctests failed" % (f, t))
+     finally:
+         set_output_comparison(oldvalue)