[Tutor] Unit testing individual modules

ALAN GAULD alan.gauld at btinternet.com
Wed Nov 20 19:37:04 CET 2013


Forwarding to the python list for visibility/commebnt.

Please always use ReplyAll to include the list.

Alan Gauld
Author of the Learn To Program website
http://www.alan-g.me.uk/

http://www.flickr.com/photos/alangauldphotos



>________________________________
> From: Patti Scott <pscott_74 at yahoo.com>
>To: Alan Gauld <alan.gauld at btinternet.com> 
>Sent: Wednesday, 20 November 2013, 17:44
>Subject: Re: [Tutor] Unit testing individual modules
> 
>
>
>Thank you for your comments. 
> 
>I am trying to import the program file named rball.py, then call/run one of the modules 
>within rball.py  [printIntro() to start]  without running the entire main() module of rball.py.   


We need to clarify the terminology.

The "program file" is both a program file (aka script) and a module.
In Python module refers to the file not the functions/classes within it.

printIntro(), and indeed main() itself, are *functions* within the module.

When you run a file directly from the OS prompt, eg:

$ python rball.py

then the interpreter will set the __name__ attribute to __main__.
Thus the if statement at the end will only execute the main() function if
you run the file from the OS prompt as above.

When you import the file, as in

import rball

Then the interpreter sets the __name__ attribute to the name 
of the module, rball in this case. Because the name is not 
__main__ your main function is defined but not executed.
Because main() is never executed the functions defined 
within it are not defined. Hence you get an error when 
you try to use them.

It might work(I have't tried it) if you execute main() 
manually and then try to execute the functions but I 
suspect it won't since the functions are local to main().

>My understanding is that being able to call/run select modules within the  
>main() module is a useful troubleshooting technique.  


No.

It is much more helpful to define your functions outside of main.
You can then access them as functions via the module, thus:

import rball
rball.printIntro()

etc.

What is useful is to define a test() or main() function that 
will exercise all of the other functions when the file is executed 
from the OS level as shown above. I suspect thats the bit that 
has confused you.

Thus when you run

$ python rball.py

You will see all your tests being executed.

But when you do an import in another program(or at the 
Python >>> prompt) you can access the functions in your 
code via the module name. This dual purpose use of files 
is a very handy feature indeed.


>According to my text, adding the conditional execution statement 
>if __name__=='__main__': main()  should result in the program 
> running when invoked directly [F5 from the Idle editor window], 
> but not running when imported [import rball].  

That is correct. 

> When imported, however, I should be able to individually test 
> the functions within rball.py  such as

>    >>import rball
>    >>rball1.gameOver(0,0)
>    False
> 
>This is not working as presented.  

Thats because you put the function definitions inside main()
Move them outside main and it will work as you expect.

HTH,

Alan G.



More information about the Tutor mailing list