mardi 6 décembre 2016

How to force MagicMock to copy function signature?

I want to mock few not important functions for the test subject (other function) - time.sleep(), etc.

I can replace them with simple mock, and this will work. But I want it to report errors if they were called with incorrect number of arguments, without named arguments, etc.

Is any way to say mock 'raise exception if your argument list is different from given function'?

Example of function I want to mock:

def mockme(arg1, arg2):
    pass

What I want:

>>> m = mock.CallableMock(signature=mockme)
>>> m(1,2)
<MagicMock name='mockme()' id='140435553563920'>
>>> m(1,2,3)
TypeError: <MagicMock name='mockme()' id='140435553563920'> takes exactly 2 arguments (3 given)

Any idea how to make this?

Aucun commentaire:

Enregistrer un commentaire