question about an exciting gotcha for unittests (and elsewhere) ...

Terry Reedy tjreedy at udel.edu
Fri Apr 23 21:05:41 CEST 2010


On 4/23/2010 8:03 AM, Cameron Simpson wrote:
> On 23Apr2010 13:25, Peter Otten<__peter__ at web.de>  wrote:

> | Move the unit tests into a separate script and have that import the module
> | cs.nodedb.node.
>
> Hmm. I have been very attracted by the idea of having the unittest in
> the module itself, and run if I "run" the module alone (instead of
> importing it).

I do this too. edit, run (F5 in IDLE), (click back to edit window) edit, 
run, ... several times in an hour. Very nice.

But... I have simpler modules (function, not class definitions) and no 
circular imports. So I was thinking the same thing Peter said for your case.

> Conversely, I've also got a few modules that are standalone programs
> and running _then_ runs the app, not a bunch of tests.
>
> Having the tests elsewhere would solve the problem as you say; an import
> of the module is what "real" code will be doing and, as my misadventure
> above demonstrates, that's a different thing as far as namespaces go.
>
> | In general, avoid importing a module and at the same time using it as the
> | main script.
>
> Fsir enough - that's a succinct description of what I did wrong.

Several people have posted problems that amount to this. An 'exciting 
gotcha' indeed ;-).

Terry Jan Reedy






More information about the Python-list mailing list