When I am writing tests or debugging code I would like to be able to add a line of Python that will notify me if that line is never executed. Is this possible?
For example, I want to be able to write code such as:
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 output to tell me that one of the branches has never been covered.
What I've 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 am struggling to find something that is activated during the compilation of a function that I would be able to hook into.
Approach 2
It is also quite straightforward if I pass in an incrementing value to each instance (e.g. check_covered(0), check_covered(1), check_covered(2),...) but this gets messy when I copy or delete code.
Approach 3
It is possible to get this information by running a code coverage tool but if possible I would prefer to do this with some simple Python code that I have a chance of understanding.
pip install coverage
and use that? It will give you a nice HMTL report that highlights missed lines etc. – Lyndonlyndsayassert False
, then if the exception never gets thrown you didn't hit that line. – Lyndonlyndsay