The trace
module provides a powerful and easy tool to trace which lines of code were executed during a run.
Tracing can be used both to ensure testing coverage and to see the behavior of our software or third-party function.
You need to perform the following steps for this recipe:
- We can implement a function that traces the execution of a provided function and returns the modules that were executed and the lines for each module:
import trace import collections def report_tracing(func, *args, **kwargs): outputs = collections.defaultdict(list) tracing = trace.Trace(trace=False) tracing.runfunc(func, *args, **kwargs) traced = collections.defaultdict(set) for filename, line in tracing.results().counts: traced[filename].add(line) for filename, tracedlines in traced.items(): with open(filename) as f: for idx, fileline in enumerate(f, start=1): outputs[filename].append( (idx, idx in...