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__)
posted @ 2013-09-22 18:06  紫红的泪  阅读(2991)  评论(0编辑  收藏  举报