0

I'm trying to write a unit test to coverage the raise of a exception, this is the code:

def delete_item(key, dynamodb=None):
    table = get_table(dynamodb)
    # delete the todo from the database
    try:
        table.delete_item(
            Key={
                'id': key
            }
        )

    except ClientError as e:
        print(e.response['Error']['Message'])
    else:
        return

I'm trying to coverage this exception with the following sentence in my unit test:

with self.assertRaises(ClientError):

    self.table.delete_item(Key={'id': ''})

but I'm getting the following error:

AssertionError: ClientError not raised

Could anybody help me?

Thanks.

  • Your function does not raise an exception - it handles it, so the test result is correct. You should check for the `print` call instead if this is what you need. Check for example [this answer](https://stackoverflow.com/a/66295431/12480730) I posted soem time ago. – MrBean Bremen Jan 12 '22 at 18:05

0 Answers0