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

Gabriel Genellina gagsl-py2 at
Fri Apr 23 21:35:55 CEST 2010

En Fri, 23 Apr 2010 03:15:16 -0300, Cameron Simpson <cs at>  
> On 23Apr2010 15:37, I wrote:

> | Experienced users will see at once what's happened: I've made a Node
> | myself in the test using the local class, and the Node class is thus
> | __main__.Node. However, my sql Backend class has independently imported
> | the "Node" and "Backend" classes from "cs.nodedb.node". So when _it_
> | calls serialise(), "Node" is "cs.nodedb.node.Node".
> [...]
> A walk around the block and I'm thinking the olny sane way to do this is
> to use relative imports, to always get the module from the same
> place as the where the test lives, and likewise in
> to relatively import to get its matching file.

Another way is to split the test code and move it into its own module.  
This way, the testing code will import the node module the same way the  
real application would do.

Gabriel Genellina

More information about the Python-list mailing list