[Tutor] Help with testing.

Kent Johnson kent37 at tds.net
Sat Jan 1 22:21:18 CET 2005


Jacob,

The module is doctest, if you look at the docs you will see examples of how to use it.

I'm not sure if there is a way to run the doctests for a single function - the doctest functions 
seem to be oriented toward testing entire modules.

To test a module selected by user input, you could do something like this:

import doctest
m = raw_input('What is the module? ')
exec 'import %s as _test_module' % m
doctest.testmod(_test_module)

However, I suggest that you set up your tests so you can run them without having to type the name of 
the module. If you run your tests much, it will get tedious to have to keep entering the name.

One way to do this is to put the test code in the main function of the module, so when you run the 
module it runs the tests. This works well for library modules that are intended to be imported. If 
you are writing an application module that is to be run as main, then you need to put the test 
driver in a separate module.

I generally write a separate test module for each module of my applications. This lets me ship the 
modules to customers without the test code.

Kent

Jacob S. wrote:
> Hi,
> 
>     A little while back somebody suggested doctesting, I think it was. Well,
> anyway, it was the testing that took testing material from the doc strings
> of functions in the format of an interpreter and the desired result (example
> below). Could anyone help by pointing me to structured documentation on how
> to use this, or even the name of the testing module(s)?
> Even nicer, a way to put the testing code in a seperate module so all I have
> to do is something like...
> 
> pseudocode
> 
> def test(obj):
>     ## Testing particulars -- parameter is a module.function that has a
> docstring with testing material
> 
> m = raw_input('What is the module? ')
> n = raw_input('What is the function? ')
> obj = eval(m+'.'+n)  ## Given that I'm the only one who will be using it and
> I will not try to crack my computer...
> test(obj)
> 
> Ohhhhh..
> and the doc string example...
> 
> 
> def function(a,b):
>     """A function to do what you want it to.
> 
>     >>> function('3','a')
>     '3a'
>     >>> function('ask','too')
>     'asktoo'
>     """
>     return a+b
> 
> 
> 
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 


More information about the Tutor mailing list