Currently I'm writing a unittest for my class function.
def test_getitem(self):
test1 = List(3)
for i in range(3):
test1.append(i)
self.assertEqual(test1[2], 2)
test1 = List(3)
for i in range(3):
test1.append(i)
self.assertRaises(IndexError, test1[4])
the problem I'm having now is at the self.assertRaises part of my code. I'm not sure if that is how it's done but when I run the unittest, it produces an error Index out of range. By right, it should be "OK".
List is my class and List(3) creates an array based list. so when i test1.append(i), it is now [0,1,2].
test1[2] is a method of calling the getitem function in my class similar to self[index].
I'm wondering if I'm assert raising correctly? self.assertEqual is fine.
assertRaises()like this you need to pass a callable as the second argument. Soself.assertRaises(IndexError, lambda: test1[4])would work, but @AK47's answer is the recommended way to do it.