Python: 测试函数是否被调用

# helper class defined elsewhere
class CallLogger(object):
   def __init__(self, meth):
     self.meth = meth
     self.was_called = False

   def __call__(self, code=None):
     self.meth()
     self.was_called = True

然后assert CallLogger的was_called为True就行了。但是这样的Callable不是个函数:

isinstance(object, types.FunctionType) # Callable will be False

对于这种Callable获取参数个数需要用:

inspect.getargspec(fn.__call__)
原文地址:https://www.cnblogs.com/codingmylife/p/3333592.html