I want to test some error handling logic, so I want to simulate a the specific exception type in my unit test. I am mocking the call to boto3, but I want to make that mock to raise a ParameterNotFound
exception. The code I am testing follows this pattern:
boto3_client = boto3.client('ssm')
try:
temp_var = boto3_client.get_parameter(Name="Something not found")['Parameter']['Value']
except boto3_client.exceptions.ParameterNotFound:
... [logic I want to test]
I have created a unittest mock, but I don't know how to make it raise the exception as this ParameterNotFound exception. I tried the following, but it doesn't work because it gets "exceptions must derive from the base class" when evaluating the except clause:
@patch('patching_config.boto3.client')
def test_sample(self, mock_boto3_client):
mock_boto3_client.return_value = mock_boto3_client
def get_parameter_side_effect(**kwargs):
raise boto3.client.exceptions.ParameterNotFound()
mock_boto3_client.get_parameter.side_effect = get_parameter_side_effect
How can I simulate a ParameterNotFound boto3 exception in my unit test?