Unit testing type comparison

Mike Wyatt mikejohnwyatt at gmail.com
Sat Aug 2 16:16:09 CEST 2008


I have a problem where type comparisons don't work in a second module when
unit tests in the first module are run.  In the example below, class Test is
declared in one.py.  When one.py is executed, it calls a method in two.py
that imports Test from one.py.  The problem is that the Test object passed
into the run method is in the "__main__" namespace, while the Test class
imported from one.py is in the "one" namespace.  Comparing type(a) and Test
returns False, even though they are both Test objects.  How can I tweak
these modules so that the type comparison returns True?

I suppose one solution is to redesign my solution to not compare types, but
I would still appreciate a solution for my personal knowledge.

**** one.py ****
class Test(object):
    pass

if __name__ == '__main__':
    import two
    two.run( Test() )

**** two.py ***
*def run( a ):
    from one import Test
    print type(a), Test, type(a) == Test
*
*** Output ***
*<class '__main__.Test'> <class 'one.Test'> False
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20080802/3e17509c/attachment.html>


More information about the Python-list mailing list