When I write tests or debugging code, I would like to add a Python line that will tell me if this line is ever executed. Is it possible?
For example, I want to write code, for example:
def f(x): if x==0: check_covered() return 1 elif x==1: check_covered() return 2 else: check_covered() return 3 f(1) f(2) print_all_missing_cases()
And I would like the result to tell me that one of the branches was never covered.
What i tried
Approach 1
I can do this for functions with a decorator as follows:
missing_fns = set() def covered(h): missing_fns.add(h.func_name) def h2(*args): missing_fns.remove(h.func_name) return h(*args) return h2 @covered def f(a): return a+1 @covered def g(a): return a+2 f(0) for x in missing_fns: print x,'is never called'
But I'm struggling to find something that activates while compiling a function that I could connect to.
Approach 2
It is also quite simple if I pass the incremental value to each instance (e.g. check_covered (0), check_covered (1), check_covered (2), ...), but it gets messy when I copy or delete the code.
Approach 3
You can get this information by running the code coverage tool, but if possible, I would prefer to do it with the simple Python code that I have for understanding.
source share