Search code examples
coverage.py

Limit coverage report to only listing which methods/functions were never called


Is there a way to have coverage only list functions that never get called? This is more to prune dead code than to enhance unit tests.

For example, given the following test.py, we can see that c will never get called.

def a():
    print("a")
    return b()

def b():
    print("b")

def c():
    print("c")

def d():
    print("d")

a()
d()

Gather stats via coverage run test.py

And indeed, I can look at the html report below and easily see that c's body never executes. However is there a report mode that just says:

test.c - not called

I looked at some of the various coverage reports modes (xml, json, lcov) and none of them seemed to show that directly.

p.s. extra bonus if it works with pytest.

enter image description here


Solution

  • There isn't a report like that, but there might be one soon. This issue is tracking the work: https://github.com/nedbat/coveragepy/issues/780