1
votes

I have a coverage report that may be lying or distorted. It says that I have coverage for a line in my Django model code. I can't see where that line is being exercised. I can see that the module is imported, that the class is imported, but not that it's being invoked/instantiated.

Thus, coverage report says I have Line A covered. Presumably that means Line B, somewhere, is exercising it. I'd like to know where Line B is. Is there a way to find the set of Line-B's (one or more) that are calling Line A, in my tests?

It seems this could be an annotation in the coverage report somehow/somewhere. It's definitely knowable, since coverage has to keep track of a thing being used.

I'm not seeing it.

If this isn't implemented, I'd like to suggest it. I know, it may be too complex as a full stack trace for each line of execution. But, maybe just the inspect of the immediate calling frame would be a good start, and helpful.

2
Just add an import traceback; traceback.print_stack() before the line in question and run tests with the -s arg.hoefling

2 Answers

1
votes

New in coverage.py 5.0 are dynamic contexts which can tell you what test ran each line of code. It won't tell you the immediate caller of the line, but it's a start.

1
votes

Here's a fun way to discover what covers that line:

Insert a bug in the line.

If you then run the tests, the ones truly covering the line will fail. The stacktraces should include Line B.