[BangPypers] unittest failing

Noufal Ibrahim noufal at gmail.com
Tue Mar 8 12:54:50 CET 2011

On Tue, Mar 08 2011, Kenneth Gonsalves wrote:

> hi,
> I am trying to figure out unittests on exceptions. I have a test which
> should pass - but it is failing. The simplest possible code is here:
> #!/usr/bin/env python
> import unittest
> def testfunc():
>     if 1:
>         raise TypeError

You should raise raise an instance of TypeError. Not the class itself. 

> class Testfunc(unittest.TestCase):
>     def testit(self):
>         self.assertRaises(TypeError,testfunc())

You should say 

        self.assertRaises(TypeError, testfunc) 

i.e. without the (). assertRaises will do the invocation.

> if __name__ == '__main__':
>     unittest.main()

I'd recommend that you use a harness like py.test (which I highly
recommend) or nose to run your tests rather than do it raw like this. 


More information about the BangPypers mailing list