Test condition coverage for a function with multiple conditions
Preconditions (2)
✅ Expected Result: All conditions inside the function have been tested for both True and False values, ensuring condition coverage.
import pytest # Function with multiple conditions to test def check_conditions(a: bool, b: bool) -> str: if a and b: return "Both True" elif a and not b: return "A True, B False" elif not a and b: return "A False, B True" else: return "Both False" # Parameterized test to cover all condition combinations @pytest.mark.parametrize("a,b,expected", [ (True, True, "Both True"), (True, False, "A True, B False"), (False, True, "A False, B True"), (False, False, "Both False") ]) def test_check_conditions(a, b, expected): result = check_conditions(a, b) assert result == expected, f"Expected {expected} but got {result}"
The function check_conditions has two boolean inputs and multiple conditions combined with and and not. To achieve condition coverage, we must test all possible True/False values for each condition.
The test uses pytest.mark.parametrize to run the test function four times with all combinations of a and b. Each test asserts the function returns the expected string for that input.
This ensures each condition (a and b) is evaluated as True and False at least once, fulfilling condition coverage requirements.
Now add data-driven testing with 3 different sets of boolean inputs and expected outputs using pytest parameterize.
condition coverage ensure in software testing?&& inside parentheses.&&. if a and b then ... uses incorrect syntax (no parentheses, wrong keywords). if (a & b) { ... } uses bitwise AND &, not logical AND. if a && b { ... } misses parentheses.if (x > 0 && y == 5). Which test inputs achieve full condition coverage?if (a || b) {
// do something
}a || b, condition coverage requires testing a and b each as true and false separately.if ((p && q) || r). Which minimal test set achieves full condition coverage?Pass the test and this pattern moves unknown