I'm doing some unit test works in python using unittest module. When i try to unittest for the custom exception, it seems like it's not working. Below is my code
# src.py
from exceptions import ClusterException, IndexingException
from utils import create_index, index_to_es
def method(bucket, key, file):
try:
s3_obj = get_object(bucket, key)
....
....
create_index(index_name, index_mapping)
index_to_es(df)
except ClusterException as e:
raise ClusterException(e)
except Exception:
raise IndexingException(e)
Here i need to test for the ClusterException exception block. So i'm mocking create_index() method to raise a ClusterException error. My testing code is
# test_src.py
with mock.patch('src.ClusterException') as mocked_cluster_exception:
mocked_cluster_exception.side_effect = ClusterException("Bad Cluster Error")
with mock.patch('src.create_index') as mocked_create_index:
mocked_create_index.side_effect = ClusterException("Index creation error")
self.assertRaises(ClusterException, method, 'bucket', 'key', 'file')
And my exception file is
# exceptions.py
class ClusterException(Exception):
pass
class IndexingException(Exception):
pass
But when i run this the testing is getting failed with below message. What am i missing here?
TypeError: catching classes that do not inherit from BaseException is not allowed