Automate statement coverage test for a simple function
Preconditions (2)
✅ Expected Result: All statements in calculate_discount function are executed and tests pass
def calculate_discount(price: float, is_member: bool) -> float: if is_member: return price * 0.9 else: return price import pytest def test_calculate_discount_member(): assert calculate_discount(100, True) == 90 def test_calculate_discount_non_member(): assert calculate_discount(100, False) == 100
The calculate_discount function applies a 10% discount if the user is a member. The tests test_calculate_discount_member and test_calculate_discount_non_member call the function with both possible values of is_member. This ensures every statement in the function runs at least once, achieving full statement coverage. Assertions check the returned price is correct. Using pytest allows easy running and reporting of test results.
Now add data-driven testing with 3 different prices and both member statuses
statement coverage measure in software testing?if (x > 0) {
print("Positive");
} else {
print("Non-positive");
}x = 5, what is the statement coverage?if (a == 10) {
print("Ten");
}
print("Done");a = 5 but reports 100% statement coverage. What is wrong?if (score >= 90) {
grade = 'A';
} else if (score >= 80) {
grade = 'B';
} else {
grade = 'C';
}Pass the test and this pattern moves unknown