[Tutor] unit testing

Magnus Lyckå magnus@thinkware.se
Sun Jun 1 11:07:01 2003


At 20:34 2003-05-31 -0700, Tom Plunket wrote:
> > But this means that you might have problems with functions
> > defined in a test module. A quick check reveals that I have
> > a global function called 'init' in four of my test suites.
>
>Oh right, I forgot about this.  Ok- that's what will make me
>worrying about this, but aren't symbols preceded by an underscore
>not linked into the global namespace on import?

Actually, as long as 'init' is only called from within a test
suite, the code in each module will find the 'init' in the
module where that test suite is defined. So, with

#a.py
def init(): print 'a init'
def a(): init()

and

#b.py
def init(): print 'b init'
def b(): init()

doing

from a import *
from b import *
a() => 'a init'
b() => 'b init'
init() => 'b init'

I still feel it's easier to understand exactly what is going
on if I don't use "from X import *".

>Oh yeah.  Ugh.  Guess I have to start remembering the Python
>keywords and modules.  :)

Using Python keywords, i.e. reserved words, as variable names
will cause syntax error, but there is a lot of builtin functions,
types and other objects. Try:

 >>> dir(__builtins__)

>Yeah, the C++ standard library is nice like that- the standard
>headers don't have .h, so I can have string.h and not screw
>anything up.  ;)  Not that I'd do anything so foolish, but still.
>:)

But in a big system there might still be name clashes when
you do #include... C++ is not good at modularity in this
regard.

>Well, unittest.main can optionally take a module name, so it
>shouldn't be too tough to make it take a list of modules.  Who do
>I talk to about permission to do this and fold it back in to the
>main Python codebase?

Look here: http://www.python.org/dev/

Unittest was developed by Steve Purcell. I don't know how
involved he is since it became a standard lib module.
http://pyunit.sourceforge.net/pyunit.html

But why not simply do:

for module in modules:
     try:
         unittest.main(module) # or whatever it is...
     except SystemExit:
         pass




--
Magnus Lycka (It's really Lyckå), magnus@thinkware.se
Thinkware AB, Sweden, www.thinkware.se
I code Python ~ The shortest path from thought to working program