[Tutor] Unit testing individual modules

Alan Gauld alan.gauld at btinternet.com
Tue Nov 19 23:58:11 CET 2013


On 19/11/13 16:06, Patti Scott wrote:
> Python 2.4
> self-study with Python Programming: An Introduction to Computer Science
> by Zelle
> I am trying to import a program that has a conditional execution
> statement at the end so I can troubleshoot individual modules in the
> main() program.

I'm not quite sure what you are doing here.
Can you give a bit more detail of how you are using this? How do you 
import it?


> Traceback (most recent call last):
>    File "<pyshell#0>", line 1, in -toplevel-
>      import rball
> ImportError: No module named rball

Is rball in the import path? (sys.path)


> (Also if I try to import with .py or .pyc)

Never include the .py or .pyc in an import

>  From the commandline interface, I can import the program, but __name__
> is still  "__main__" and I haven't successfully unit tested any of the
> individual functions.

This bit confuses me. Which command line do you mean?
The OS or Python interpreter?

Also the only way you can test the functions is if name is main.
That's because the functions are all defined inside main().
That's a highly unusual pattern...


> # rball.py
> # textbook example of top-down design for Monte Carlo raquetball simulation
> from random import random

This import is the only thing that happens if name is not main...

> def main():
>      def printIntro():...
>      def getInputs():...
>      def simNGames(n, probA, probB):...
>      def simOneGame(probA, probB):...
>      def gameOver(a,b):...
>      def printSummary(winsA, winsB):...
 >      ...
 >
> if __name__ == "__main__": main()

Are you really sure that's what you want?

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos



More information about the Tutor mailing list