[Python-checkins] python/dist/src/Lib/test sample_doctest.py, NONE, 1.1.2.1 test_doctest.txt, NONE, 1.1.2.1 test_doctest2.txt, NONE, 1.1.2.1 test_doctest.py, 1.5.18.9, 1.5.18.10

dcjim at users.sourceforge.net dcjim at users.sourceforge.net
Thu Aug 5 23:38:53 CEST 2004


Update of /cvsroot/python/python/dist/src/Lib/test
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv28896/test
Modified Files:
 Tag: tim-doctest-branch
	test_doctest.py 
Added Files:
 Tag: tim-doctest-branch
	sample_doctest.py test_doctest.txt test_doctest2.txt 
Log Message:
- Added DocFileSuite
- Added a script_from_examples method to convert examples to a script.
- Changed the way the debugger works to include all of the text around
 the exaples. Having the explanatory text can make debugging a lot
 easier.
- Added a DebugRunner for that raises an error on the first unexpected
 error or failure.
- Wrote tests for DocTestSuite, DocFileSuite, script_from_examples, 
 and DebugRunner.
- Added a debug method to DocTestCase that uses the DebugRunner to
 raise an error on the first failure, to support post-mortem
 debugging.
--- NEW FILE: sample_doctest.py ---
"""This is a sample module that doesn't really test anything all that
 interesting
It simply has a few tests, some of which suceed and some of which fail.
It's important that the numbers remain constance, as another test is
testing the running of these tests.
>>> 2+2
4
"""
def foo():
 """
 >>> 2+2
 5
 >>> 2+2
 4
 """
def bar():
 """
 >>> 2+2
 4
 """
def test_silly_setup():
 """
 >>> import test.test_doctest
 >>> test.test_doctest.sillySetup
 True
 """
def w_blank():
 """
 >>> if 1:
 ... print 'a'
 ... print
 ... print 'b'
 a
 <BLANKLINE>
 b
 """
x = 1
def x_is_one():
 """
 >>> x
 1
 """
def y_is_one():
 """
 >>> y
 1
 """
def test_suite():
 import doctest
 return doctest.DocTestSuite()
--- NEW FILE: test_doctest.txt ---
This is a sample doctest in a text file.
In this example, we'll rely on a global variable being set for us
already:
 >>> favorite_color
 'blue'
We can make this fail by disabling the blank-line feature.
 >>> if 1:
 ... print 'a'
 ... print
 ... print 'b'
 a
 <BLANKLINE>
 b
--- NEW FILE: test_doctest2.txt ---
This is a sample doctest in a text file.
In this example, we'll rely on some silly setup:
 >>> import test.test_doctest
 >>> test.test_doctest.sillySetup
 True
Index: test_doctest.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/test/test_doctest.py,v
retrieving revision 1.5.18.9
retrieving revision 1.5.18.10
diff -C2 -d -r1.5.18.9 -r1.5.18.10
*** test_doctest.py	4 Aug 2004 14:06:27 -0000	1.5.18.9
--- test_doctest.py	5 Aug 2004 21:38:50 -0000	1.5.18.10
***************
*** 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,927 ****
 
 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'
 >>> print doctest.testsource(test.test_doctest, name)
 print '1\n2\n3'
 # Expected:
--- 917,940 ----
 
 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'
 >>> print doctest.testsource(test.test_doctest, name)
+ #
 print '1\n2\n3'
 # Expected:
***************
*** 929,935 ****
--- 942,950 ----
 # 2
 # 3
+ #
 
 >>> name = 'test.test_doctest.SampleClass.a_classmethod'
 >>> print doctest.testsource(test.test_doctest, name)
+ #
 print SampleClass.a_classmethod(10)
 # Expected:
***************
*** 938,941 ****
--- 953,957 ----
 # Expected:
 # 12
+ #
 """
 
***************
*** 976,979 ****
--- 992,1160 ----
 """
 
+ 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

AltStyle によって変換されたページ (->オリジナル) /