Unit testing beginner question

2011-05-23 Thread Andrius
Hello,

would be gratefull for the explonation.

I did a simple test case:

def setUp(self):
  self.testListNone = None

def testListSlicing(self):
  self.assertRaises(TypeError, self.testListNone[:1])

and I am expecting test to pass, but I am getting exception:
Traceback (most recent call last):
self.assertRaises(TypeError, self.testListNone[:1])
TypeError: 'NoneType' object is unsubscriptable

I thought that assertRaises will pass since TypeError exception will
be raised?

Ta,
Andrius



-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Unit testing beginner question

2011-05-23 Thread Andrius A
That was quick! Thanks Ian


On 23 May 2011 23:46, Ian Kelly  wrote:

> On Mon, May 23, 2011 at 4:30 PM, Andrius  wrote:
> > and I am expecting test to pass, but I am getting exception:
> > Traceback (most recent call last):
> >self.assertRaises(TypeError, self.testListNone[:1])
> > TypeError: 'NoneType' object is unsubscriptable
> >
> > I thought that assertRaises will pass since TypeError exception will
> > be raised?
>
> The second argument to assertRaises must be a function that
> assertRaises will call.  assertRaises can't catch the error above
> because it is raised when the argument is evaluated, before
> assertRaises has even been called.
>
> This would work:
>
> self.assertRaises(TypeError, lambda: self.testListNone[:1])
>
> Cheers,
> Ian
>
-- 
http://mail.python.org/mailman/listinfo/python-list