Ned Batchelder: Caller-specifi... Note

Ned Batchelder: Caller-specific coverage

The author desires to measure code coverage for a function on a per-caller basis. This is motivated by refactoring error-checking code in their BASIC interpreter, Acidica. Initially, each built-in function had its own argument validation, allowing specific coverage of error conditions. After refactoring, this detailed coverage was lost as validation moved to a shared helper function.The proposed solution involves a decorator that leverages coverage.py's dynamic contexts. This decorator creates a new coverage context named after the calling site (file and line number) before executing the decorated function. Upon function completion, the original context is restored.A proof-of-concept decorator using coverage.Coverage.current() and cov.switch_context() demonstrates this functionality. This requires a minor, unreleased change to coverage.py to support nested contexts. The author found that their HTML coverage reports now show distinct contexts for each caller of the expects function, revealing which branches were executed by whom.Future improvements include post-processing these contexts to identify missing coverage for specific callers and enabling hierarchical or multiple contexts to track both caller and execution details simultaneously. The author also aims to integrate this functionality via coverage configuration rather than modifying source code with decorators. Potential applications include using function arguments, like func_name, as context identifiers.
CdXz5zHNQW_IFrCzZRuFh.png