So I have class Dog:
def __init__(self, res):
self._res = res
self.dog = Dog([1,2])
When Python runs the line
self.assertRaises(IndexError, self.dog.error()), it has to calculate the arguments of the method
self.assertRaises before passing them to it, so it calls
self.dog.error() which raises an error. This is how all functions work. The presence of
self.assertRaises does nothing to stop the error being raised. You are meant to call
self.assertRaises(IndexError, self.dog.error), where the second argument is now the actual function/method object
self.dog.error without having called it yet.
self.assertRaises will call it for you wrapped in a try/except so that it can do the test properly.
with self.assertRaises(IndexError): self.dog.error()