Various solutions are possible for this thing. But first, I must point out that unit testing is for unit testing; that unit may be a matter of viewing things. In your case, I would say that the unit is the function a()
, because you want to check that your device is behaving correctly, that is, it calls the function b()
in the right place. Another idea would be to say that the functions a()
and b()
are one. Then you do not like to check if the function b()
called, you just want to check the result of calling the function a()
. Make your own mind that suits your business best.
If you really want to test the function a()
as a unit, you must prepare your device for testing. In your case, this can be done by adding an additional argument to it (which is used by function b
by default) and which will be used instead of the hard encoding function b()
in a()
:
def a(self, subfunction=None): is subfunction is None:
Now you can enter (during testing) an auxiliary function that simply informs the test that it called:
store = None def injected(*args, **kwargs): global store store = args, kwargs obj.a(subfunction=injected)
Alfe source share