0
0
Testing Fundamentalstesting~15 mins

Condition coverage in Testing Fundamentals - Build an Automation Script

Choose your learning style9 modes available
Test condition coverage for a function with multiple conditions
Preconditions (2)
Step 1: Call the function with inputs (True, True)
Step 2: Call the function with inputs (True, False)
Step 3: Call the function with inputs (False, True)
Step 4: Call the function with inputs (False, False)
Step 5: Verify that each condition in the function has been evaluated to both True and False at least once
✅ Expected Result: All conditions inside the function have been tested for both True and False values, ensuring condition coverage.
Automation Requirements - pytest
Assertions Needed:
Assert the function returns expected output for each input combination
Assert that all conditions in the function are exercised with True and False values
Best Practices:
Use parameterized tests to cover all input combinations
Keep tests simple and focused on one function
Use clear and descriptive test names
Automated Solution
Testing Fundamentals
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.

Common Mistakes - 3 Pitfalls
Testing only some input combinations instead of all
Using print statements instead of assertions
Hardcoding expected outputs without understanding conditions
Bonus Challenge

Now add data-driven testing with 3 different sets of boolean inputs and expected outputs using pytest parameterize.

Show Hint