I am unit-testing my software with PyUnit and find it unfortunate that the assertions provided by mock objects (e.g. mock.assert_called_with) don't seem to provide a way to set a message in case the assertion fails. That's why I want to use a wrapper like this:
def wrap_mock_msg(mock_method, *args, msg):
try:
mock_method(<args here!>)
except AssertionError as e:
raise AssertionError(e.args, msg)
It takes the assertion method of a given mock object as mock_method, the message to be displayed upon failure as msg, and a series of arguments to be passed to the mock_method as args. I need it to be a variable args list, since mock.assert_called_with(arg1, arg2, .., arg_n) can also take any number of arguments, depending on the method I'm trying to mock.
Unfortunately, I can't just pass a list of arguments to the mock_method, since it will of course be taken as a single argument. Now, this leads me to the challenge of having to pass the list of arguments to the mock_method as if I had hard-typed it. For example:
args = ['an', 'example']
..should result in the following call:
mock_method('an', 'example')
Is there any way to achieve this?