[Tutor] doctest

Kent Johnson kent37 at tds.net
Sun Jan 2 15:51:24 CET 2005


Jacob S. wrote:
> Even so, doctest doesn't seem to recognize the module level docstring.
> It will run the test inside the functions, but it says there isn't a test on
> the module level.
> I put the docstring just like in the example at the link you provided...

Please post the code for the module you are testing.

> 
> Also, anything I can do... Presently, since I'm running windows xp, I would
> have to hunt for the command prompt and type in the command
> 
> '"C:\python24\python.exe" "C:\documents and settings\jacob\desktop\working
> python programs\testmodules.py" -v'

You should put C:\python24 in your PATH environment variable. Not sure how to do that on XP; on Win2K I
- right-click My Computer and select Properties
- click the Advanced tab
- click Environment Variables
- find the System variable Path and edit it
- append ";C:\python24" to Path (no quotes; the semicolon is needed, it is a separator for the Path 
variable)
- click OK a few times
- restart your DOS shell so it gets the new variable

Now you should be able to open a command line in the directory containing your program and type
  > python testmodules.py -v


Oh, the light goes on...you want to be able to just double-click testmodules.py. See below for the 
fix for -v

> 
> ...or make a batch file to do it for me...
> How can I make testmodules.py (shown below) append the -v to itself? Is
> there a self.results or something in testmod?

 From the docs at http://docs.python.org/lib/doctest-basic-api.html:
"Optional argument verbose prints lots of stuff if true"
So try
   doctest.testmod(eval(modtotest), verbose=True)

> 
> ## testmodules.py ###########
> import doctest
> 
> modtotest = 'FractionReducer2'
> 
> exec "import %s" % modtotest
> doctest.testmod(eval(modtotest))
> raw_input()
> #############################

Since you will be editing this to change the module under test, I don't think there is any benefit 
to putting the module name in a string. I would write it like this:

import doctest
import FractionReducer2 as test_module
doctest.testmod(test_module, verbose=True)
raw_input()

This preserves the single point of change when you want to test a different module, but it is IMO 
much more readable.

Kent


More information about the Tutor mailing list