Skip to content Skip to sidebar Skip to footer

How Do You Pass Exception Arguments To Python Unittest Mock Side Effect?

How do you pass exceptions that require arguments as mock side_effects? I'm trying to test for assertRaises of boto.exception.EC2ResponsError, but get a 'TypeError: init() takes

Solution 1:

As documented in Calling section you can use both instance or class in side_effect initialization. Moreover you can use a callable that raise your desired exception.

When class is used to define side_effect and the desired exception has not the trivial empty constructor you will get an exception like the one you had because mock framework doesn't know how to build that exception.

In your case you can use something like

mock_connect.side_effect = boto.exception.EC2ResponseError(400, "My reason", "my body")

Post a Comment for "How Do You Pass Exception Arguments To Python Unittest Mock Side Effect?"