Example: Code Instrumentation for Coverage Measurement
def example_function(x):
if x > 0:
print("Positive")
else:
print("Non-Positive")
return x * 2# Initialize coverage tracking global variable
branch_coverage = {
"example_function_1": False, # if branch for x > 0
"example_function_2": False # else branch
}
def example_function(x):
if x > 0:
branch_coverage["example_function_1"] = True
print("Positive")
else:
branch_coverage["example_function_2"] = True
print("Non-Positive")
return x * 2
def print_coverage():
for branch, hit in branch_coverage.items():
print(f"{branch} was {'hit' if hit else 'not hit'}")
# Example usage
result = example_function(5)
print_coverage()
result = example_function(-3)
print_coverage()Last updated