Track invocations of methods and functions

45 views Asked by At

I'm looking for the library which allows to track invocation of methods and functions. Think of it as of Mock providing called and call_count properties.

Example of end-result needed:

s = MagicProxyLib()

@s
class MyClass:

    def not_called(self):
        print("This is not called")

    def first_method(self):
        print("First is called")

    def second_method(self):
        print("Second is called")


mc = MyClass()
mc.first_method()
mc.second_method()
mc.second_method()

I can implement such a decorator myself, but do not want reinvent the wheel if there is already some library with similar functionality.

I expect to be able to use this library is a such way

assert not s.called(mc.not_called)
assert s.called(mc.first_method)
assert s.call_count(mc.second_method) == 2

I have checked this answer but profiling/tracing does not quite serve the same purpose as here. Thanks for you package suggestions.

1

There are 1 answers

0
jsbueno On

You likely want a profiler - they can collect all this usage statistics. A nice one is shipped along with Python - check: https://docs.python.org/3/library/profile.html