Test branch coverage for a simple function
Preconditions (2)
✅ Expected Result: Both branches of the function are executed and verified, ensuring full branch coverage
def is_positive(number: int) -> str: if number > 0: return "Positive" else: return "Non-positive" import pytest def test_is_positive_true_branch(): result = is_positive(5) assert result == "Positive" def test_is_positive_false_branch(): result = is_positive(0) assert result == "Non-positive" result = is_positive(-3) assert result == "Non-positive"
The function is_positive has two branches: one for numbers greater than zero, and one for zero or less.
The first test test_is_positive_true_branch calls the function with a positive number to cover the true branch and asserts the output is "Positive".
The second test test_is_positive_false_branch calls the function with zero and a negative number to cover the false branch and asserts the output is "Non-positive".
This way, both branches are executed and verified, achieving full branch coverage.
Now add data-driven testing with 3 different inputs to cover branches
if (a > 0) {
if (b == 5) {
print('Path 1');
} else {
print('Path 2');
}
} else {
print('Path 3');
}if (x != 0) {
print('Non-zero');
}if (score >= 90) {
grade = 'A';
} else if (score >= 75) {
grade = 'B';
} else {
grade = 'C';
}Pass the test and this pattern moves unknown