[Tutor] unittest: unbound method?

Alan Gauld alan.gauld at freenet.co.uk
Sun Nov 14 19:46:42 CET 2004


> from panTools import Ini
> class TestIni(unittest.TestCase):
> ...
>     def test_loadDict(self):
>         Ini.load(self.name)   #<===========
>
> in test_loadDict
>     #x=Ini.load(self.name)
> TypeError: unbound method load() must be called with Ini instance as
first
> argument (got dict instance instead)
>
> I read several unittest tutorials and they all use class.method,
> not instance.method. What's going wrong in my code?

The error is with Ini which is a class you imported from panTools.
You need to use an instance of Ini in your class method:

 def test_loadDict(self):
      i = Ini()   # create instance
      i.load(self.name)  # use instance instead of class.

Personally I usually test new classes at the >>> prompt rather than
writing test harnesses using unittest. The >>> prompt is much more
immediate. Once your individual classes work you can use unittest
to check the bigger picture combinations of classes.

Alan G.



More information about the Tutor mailing list