From ebfb2f46c9cd2c12fba86b18b19789656491f4ed Mon Sep 17 00:00:00 2001 From: Michael Hanselmann <hansmi@google.com> Date: Tue, 13 Nov 2012 20:10:32 +0100 Subject: [PATCH] Add test utility to count calls to function In some cases it's nice to verify a function has been called exactly N times. This is going to be used in tests for remote commands. Signed-off-by: Michael Hanselmann <hansmi@google.com> Reviewed-by: Iustin Pop <iustin@google.com> --- test/testutils.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/test/testutils.py b/test/testutils.py index 1a47a66b0..3fcfbc478 100644 --- a/test/testutils.py +++ b/test/testutils.py @@ -223,3 +223,32 @@ def UnifyValueType(data): for (key, value) in data.iteritems()]) return data + + +class CallCounter(object): + """Utility class to count number of calls to a function/method. + + """ + def __init__(self, fn): + """Initializes this class. + + @type fn: Callable + + """ + self._fn = fn + self._count = 0 + + def __call__(self, *args, **kwargs): + """Calls wrapped function with given parameters. + + """ + self._count += 1 + return self._fn(*args, **kwargs) + + def Count(self): + """Returns number of calls. + + @rtype: number + + """ + return self._count -- GitLab