class FuncWrapper(object):

    def __init__(self, func):
        self.call_count = 0
self.func = func

def __call__(self, *args, **kwargs):
    self.call_count += 1
return self.func(*args, **kwargs)


class CounterTest(unittest.TestCase):

    def test_call_count(self):
        c = Counter()
new_call = FuncWrapper(c.increment)
with mock.patch.object(c, 'increment', new=new_call) as fake_increment:
    print(fake_increment)
self.assertEqual(0, fake_increment.call_count)
c.increment()
self.assertEqual(1, fake_increment.call_count)
self.assertEqual(1, c.count)
Using python mock to count number of method calls