I'm new to Python and I have just started learning how to use classes. I implemented an array-based list with a max size of 50. I also have an append method where self.count refers to the next available position in the list. Now I'm trying to write a unit test for my append method but I'm wondering, how do I check for the assertion error other than appending 50 times? Is there a way to manually change my self.count?
This is my append method.
def append(self,item):
assert self.count<=50
if self.count>50:
raise AssertionError("Array is full")
self.array[self.count]=item
self.count+=1
This is what I tried for my unit test:
def testAppend(self):
a_list=List()
a_list.append(2)
self.assertEqual(a_list[0],2)
# test for assertion error
Any help will be appreciated!
EDIT: Okay after all the useful suggestions I realised I'm supposed to raise an exception instead.
def append(self,item):
try:
self.array[self.count]=item
except IndexError:
print('Array is full')
self.count+=1
Now this is my unit test but I got the warning
Warning (from warnings module):
File "C:\Users\User\Desktop\task1unitTest.py", line 57
self.assertRaises(IndexError,a_list.append(6))
DeprecationWarning: callable is None
.......
def testAppend(self):
a_list=List()
a_list.append(2)
self.assertEqual(a_list[0],2)
a_list.count=51
self.assertRaises(IndexError,a_list.append(6))