question about an exciting gotcha for unittests (and elsewhere) ...
Peter Otten
__peter__ at web.de
Fri Apr 23 07:25:26 EDT 2010
Cameron Simpson wrote:
> and it's failing. I've traced the failure cause, ending up with this
> assertion message from the end of serialise() above:
>
> AssertionError: <class '__main__.Node'> HOST:foo:{} <class
> 'cs.nodedb.node.Node'>
>
> 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".
>
> And lo, the:
>
> if isinstance(value, Node):
>
> test at the top of serialise() fails.
>
> What's a sensible way of doing this correctly?
Move the unit tests into a separate script and have that import the module
cs.nodedb.node.
In general, avoid importing a module and at the same time using it as the
main script. When persistent objects are involved "the same time" can even
spread over distinct runs of separate scripts accessing the same data.
Obvious? But you asked for a /sensible/ way.
Peter
More information about the Python-list
mailing list