[Python-checkins] python/dist/src/Lib/test sample_doctest.py, 1.1, 1.2 test_doctest.txt, 1.1, 1.2 test_doctest2.txt, 1.1, 1.2 test_doctest.py, 1.7, 1.8

tim_one at users.sourceforge.net tim_one at users.sourceforge.net
Sat Aug 7 00:03:03 CEST 2004


Update of /cvsroot/python/python/dist/src/Lib/test
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv23152/Lib/test

Modified Files:
	test_doctest.py 
Added Files:
	sample_doctest.py test_doctest.txt test_doctest2.txt 
Log Message:
Merging from tim-doctest-branch, which is now closed.
This primarily adds more powerful ways to work with unittest, including
spiffy support for building suites out of doctests in non-Python
"text files".





Index: test_doctest.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/test/test_doctest.py,v
retrieving revision 1.7
retrieving revision 1.8
diff -C2 -d -r1.7 -r1.8
*** test_doctest.py	4 Aug 2004 20:04:32 -0000	1.7
--- test_doctest.py	6 Aug 2004 22:02:59 -0000	1.8
***************
*** 12,17 ****
--- 12,21 ----
  def sample_func(v):
      """
+     Blah blah
+ 
      >>> print sample_func(22)
      44
+ 
+     Yee ha!
      """
      return v+v
***************
*** 253,257 ****
      >>> e = tests[0].examples[0]
      >>> print (e.source, e.want, e.lineno)
!     ('print sample_func(22)', '44\n', 1)
  
      >>> doctest: -ELLIPSIS # Turn ellipsis back off
--- 257,261 ----
      >>> e = tests[0].examples[0]
      >>> print (e.source, e.want, e.lineno)
!     ('print sample_func(22)', '44\n', 3)
  
      >>> doctest: -ELLIPSIS # Turn ellipsis back off
***************
*** 913,924 ****
  
  The testsource() function takes a module and a name, finds the (first)
! test with that name in that module, and converts it to an
  
      >>> import test.test_doctest
      >>> name = 'test.test_doctest.sample_func'
      >>> print doctest.testsource(test.test_doctest, name)
      print sample_func(22)
      # Expected:
      #     44
  
      >>> name = 'test.test_doctest.SampleNewStyleClass'
--- 917,934 ----
  
  The testsource() function takes a module and a name, finds the (first)
! test with that name in that module, and converts it to a script. The
! example code is converted to regular Python code.  The surrounding
! words and expected output are converted to comments:
  
      >>> import test.test_doctest
      >>> name = 'test.test_doctest.sample_func'
      >>> print doctest.testsource(test.test_doctest, name)
+     #      Blah blah
+     #
      print sample_func(22)
      # Expected:
      #     44
+     #
+     #      Yee ha!
  
      >>> name = 'test.test_doctest.SampleNewStyleClass'
***************
*** 976,979 ****
--- 986,1154 ----
  """
  
+ def test_DocTestSuite():
+     """DocTestSuite creates a unittest test suite into a doctest.
+ 
+        We create a Suite by providing a module.  A module can be provided
+        by passing a module object:
+ 
+          >>> import unittest
+          >>> import test.sample_doctest
+          >>> suite = doctest.DocTestSuite(test.sample_doctest)
+          >>> suite.run(unittest.TestResult())
+          <unittest.TestResult run=7 errors=0 failures=3>
+ 
+        We can also supply the module by name:
+ 
+          >>> suite = doctest.DocTestSuite('test.sample_doctest')
+          >>> suite.run(unittest.TestResult())
+          <unittest.TestResult run=7 errors=0 failures=3>
+ 
+        We can use the current module:
+ 
+          >>> suite = test.sample_doctest.test_suite()
+          >>> suite.run(unittest.TestResult())
+          <unittest.TestResult run=7 errors=0 failures=3>
+ 
+        We can supply global variables.  If we pass globs, they will be
+        used instead of the module globals.  Here we'll pass an empty
+        globals, triggering an extra error:
+ 
+          >>> suite = doctest.DocTestSuite('test.sample_doctest', globs={})
+          >>> suite.run(unittest.TestResult())
+          <unittest.TestResult run=7 errors=0 failures=4>
+ 
+        Alternatively, we can provide extra globals.  Here we'll make an
+        error go away by providing an extra global variable:
+ 
+          >>> suite = doctest.DocTestSuite('test.sample_doctest',
+          ...                              extraglobs={'y': 1})
+          >>> suite.run(unittest.TestResult())
+          <unittest.TestResult run=7 errors=0 failures=2>
+ 
+        You can pass option flags.  Here we'll cause an extra error
+        by disabling the blank-line feature:
+ 
+          >>> suite = doctest.DocTestSuite('test.sample_doctest',
+          ...                         optionflags=doctest.DONT_ACCEPT_BLANKLINE)
+          >>> suite.run(unittest.TestResult())
+          <unittest.TestResult run=7 errors=0 failures=4>
+ 
+        You can supply setUp and teatDoen functions:
+ 
+          >>> def setUp():
+          ...     import test.test_doctest
+          ...     test.test_doctest.sillySetup = True
+ 
+          >>> def tearDown():
+          ...     import test.test_doctest
+          ...     del test.test_doctest.sillySetup
+ 
+        Here, we installed a silly variable that the test expects:
+ 
+          >>> suite = doctest.DocTestSuite('test.sample_doctest',
+          ...      setUp=setUp, tearDown=tearDown)
+          >>> suite.run(unittest.TestResult())
+          <unittest.TestResult run=7 errors=0 failures=2>
+ 
+        But the tearDown restores sanity:
+ 
+          >>> import test.test_doctest
+          >>> test.test_doctest.sillySetup
+          Traceback (most recent call last):
+          ...
+          AttributeError: 'module' object has no attribute 'sillySetup'
+ 
+        Finally, you can provide an alternate test finder.  Here we'll
+        use a custom test_finder to to run just the test named bar:
+ 
+          >>> finder = doctest.DocTestFinder(
+          ...    namefilter=lambda prefix, base: base!='bar')
+          >>> suite = doctest.DocTestSuite('test.sample_doctest',
+          ...                              test_finder=finder)
+          >>> suite.run(unittest.TestResult())
+          <unittest.TestResult run=2 errors=0 failures=0>
+ 
+        """
+ 
+ def test_DocFileSuite():
+     """We can test tests found in text files using a DocFileSuite.
+ 
+        We create a suite by providing the names of one or more text
+        files that include examples:
+ 
+          >>> import unittest
+          >>> suite = doctest.DocFileSuite('test_doctest.txt',
+          ...                              'test_doctest2.txt')
+          >>> suite.run(unittest.TestResult())
+          <unittest.TestResult run=2 errors=0 failures=2>
+ 
+        The test files are looked for in the directory containing the
+        calling module.  A package keyword argument can be provided to
+        specify a different relative location.
+ 
+          >>> import unittest
+          >>> suite = doctest.DocFileSuite('test_doctest.txt',
+          ...                              'test_doctest2.txt',
+          ...                              package='test')
+          >>> suite.run(unittest.TestResult())
+          <unittest.TestResult run=2 errors=0 failures=2>
+ 
+        Note that '/' should be used as a path separator.  It will be
+        converted to a native separator at run time:
+ 
+ 
+          >>> suite = doctest.DocFileSuite('../test/test_doctest.txt')
+          >>> suite.run(unittest.TestResult())
+          <unittest.TestResult run=1 errors=0 failures=1>
+ 
+        You can specify initial global variables:
+ 
+          >>> suite = doctest.DocFileSuite('test_doctest.txt',
+          ...                              'test_doctest2.txt',
+          ...                              globs={'favorite_color': 'blue'})
+          >>> suite.run(unittest.TestResult())
+          <unittest.TestResult run=2 errors=0 failures=1>
+ 
+        In this case, we supplied a missing favorite color. You can
+        provide doctest options:
+ 
+          >>> suite = doctest.DocFileSuite('test_doctest.txt',
+          ...                              'test_doctest2.txt',
+          ...                         optionflags=doctest.DONT_ACCEPT_BLANKLINE,
+          ...                              globs={'favorite_color': 'blue'})
+          >>> suite.run(unittest.TestResult())
+          <unittest.TestResult run=2 errors=0 failures=2>
+ 
+        And, you can provide setUp and tearDown functions:
+ 
+        You can supply setUp and teatDoen functions:
+ 
+          >>> def setUp():
+          ...     import test.test_doctest
+          ...     test.test_doctest.sillySetup = True
+ 
+          >>> def tearDown():
+          ...     import test.test_doctest
+          ...     del test.test_doctest.sillySetup
+ 
+        Here, we installed a silly variable that the test expects:
+ 
+          >>> suite = doctest.DocFileSuite('test_doctest.txt',
+          ...                              'test_doctest2.txt',
+          ...                              setUp=setUp, tearDown=tearDown)
+          >>> suite.run(unittest.TestResult())
+          <unittest.TestResult run=2 errors=0 failures=1>
+ 
+        But the tearDown restores sanity:
+ 
+          >>> import test.test_doctest
+          >>> test.test_doctest.sillySetup
+          Traceback (most recent call last):
+          ...
+          AttributeError: 'module' object has no attribute 'sillySetup'
+ 
+     """
+ 
+ 
  ######################################################################
  ## Main



More information about the Python-checkins mailing list