Skip to content
Advertisement

AssertEqual failing when comparing two seemingly identical IndexErrors

I have a DynamicArray class shown below. (I have only included relevant methods. The rest can be viewed from https://www.geeksforgeeks.org/implementation-of-dynamic-array-in-python/)

JavaScript

Then I have another unit test file down below

JavaScript

When I run the test I expect self.a.__getitem__(0) to throw IndexError('0 is out of bounds') and I can’t see why the assertion fails? The only difference is that self.a.__getitem__(0) will yield IndexError('{} is out of bounds'.format(0)), which seems to me the same as IndexError('0 is out of bounds')

I tried running below code to see if the string by themselves were any different

JavaScript

and confirmed that only the third if statement did not print

below is the photo of the console

enter image description here

Thanks in advance. Constructive criticisms and feedbacks are welcome.

Advertisement

Answer

Exceptions can’t be conpared with assertEqual.

JavaScript

And Exceptions must be raiseed to be captured.
You’re returning IndexError

JavaScript

https://docs.python.org/3/library/unittest.html#unittest.TestCase.assertRaises

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement