Bird
Raised Fist0
Testing Fundamentalstesting~20 mins

Branch coverage in Testing Fundamentals - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Branch Coverage Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
Understanding Branch Coverage Basics

Which statement best describes branch coverage in software testing?

AIt checks if all functions in the program have been called at least once.
BIt counts how many lines of code have been executed during testing.
CIt measures whether each possible branch (true/false) of every decision point has been executed at least once.
DIt verifies that all variables have been assigned values during testing.
Attempts:
2 left
💡 Hint

Think about decisions in code like if statements and their possible paths.

❓ Predict Output
intermediate
1:30remaining
Branch Coverage Test Result

Consider this Python function:

def check_number(x):
    if x > 0:
        return "Positive"
    else:
        return "Non-positive"

If a test suite calls check_number(5) only, what is the branch coverage?

A75% - partial testing of branches
B50% - only the 'x > 0' true branch was tested
C0% - no branches were tested
D100% - all branches were tested
Attempts:
2 left
💡 Hint

Think about which parts of the if statement run when x=5.

❓ assertion
advanced
2:00remaining
Correct Assertion for Branch Coverage

Given this test code snippet for a function is_even(n) that returns True if n is even, which assertion best checks that both branches of the function are tested?

Testing Fundamentals
def test_is_even():
    # Which assertions ensure full branch coverage?
    pass
Aassert is_even(0) == True
Bassert is_even(2) == True
Cassert is_even(3) == False
Dassert is_even(2) == True and is_even(3) == False
Attempts:
2 left
💡 Hint

To cover branches, test both even and odd inputs.

🔧 Debug
advanced
2:00remaining
Identify Branch Coverage Issue in Test Cases

Here is a function and its test cases:

def classify_age(age):
    if age < 13:
        return "Child"
    elif age < 20:
        return "Teen"
    else:
        return "Adult"

# Test cases
assert classify_age(10) == "Child"
assert classify_age(25) == "Adult"

What is the branch coverage status of these tests?

AThe tests miss the 'Teen' branch, so branch coverage is incomplete.
BAll branches are covered by the tests.
CThe tests miss the 'Child' branch, so coverage is incomplete.
DThe tests miss the 'Adult' branch, so coverage is incomplete.
Attempts:
2 left
💡 Hint

Check if all if and elif branches have been tested.

❓ framework
expert
2:00remaining
Measuring Branch Coverage with a Testing Framework

Using Python's unittest and coverage tools, which command correctly measures branch coverage for test_module.py?

Acoverage run --branch -m unittest test_module.py
Bcoverage run -m unittest test_module.py
Cunittest --branch test_module.py
Dcoverage branch run -m unittest test_module.py
Attempts:
2 left
💡 Hint

Check the coverage tool's option for branch coverage.

Practice

(1/5)
1. What does branch coverage testing ensure in software testing?
easy
A. All possible paths of each decision point are tested
B. Only the main function is tested
C. All variables are initialized
D. The software runs faster

Solution

  1. Step 1: Understand branch coverage concept

    Branch coverage means testing every decision point in the code, including both true and false outcomes.
  2. Step 2: Match concept to options

    All possible paths of each decision point are tested correctly states that all possible paths of each decision point are tested, which is the essence of branch coverage.
  3. Final Answer:

    All possible paths of each decision point are tested -> Option A
  4. Quick Check:

    Branch coverage = all decision paths tested [OK]
Hint: Branch coverage tests both true and false paths [OK]
Common Mistakes:
  • Confusing branch coverage with statement coverage
  • Thinking it only tests main functions
  • Assuming it improves speed
2. Which of the following is the correct way to write a branch coverage test condition in pseudocode?
easy
A. print('Test complete')
B. while x > 10 do A
C. for i in range(10): do A
D. if (x > 10) then do A else do B

Solution

  1. Step 1: Identify branch decision structure

    Branch coverage requires a decision point with two or more paths, like an if-else condition.
  2. Step 2: Check options for decision points

    if (x > 10) then do A else do B shows an if-else statement, which has two branches (true and false). Other options do not have decision branches.
  3. Final Answer:

    if (x > 10) then do A else do B -> Option D
  4. Quick Check:

    If-else = branch decision [OK]
Hint: Look for if-else to identify branches [OK]
Common Mistakes:
  • Choosing loops as branch examples
  • Ignoring else part of if-else
  • Confusing print statements with branches
3. Consider the following code snippet:
if (a > 0) {
  if (b == 5) {
    print('Path 1');
  } else {
    print('Path 2');
  }
} else {
  print('Path 3');
}

How many test cases are needed for 100% branch coverage?
medium
A. 3
B. 4
C. 2
D. 5

Solution

  1. Step 1: Identify decision points and branches

    There are two if statements: outer (a > 0) with true/false branches, inner (b == 5) with true/false branches.
  2. Step 2: Count unique branches

    Outer if: 2 branches (true, false). Inner if (only if outer true): 2 branches. Total branches = 3 unique paths: Path 1, Path 2, Path 3.
  3. Final Answer:

    3 -> Option A
  4. Quick Check:

    Branches = 3 paths [OK]
Hint: Count all true/false branches in nested ifs [OK]
Common Mistakes:
  • Counting only outer if branches
  • Assuming each if adds 2 test cases independently
  • Ignoring nested structure
4. You have this code:
if (x != 0) {
  print('Non-zero');
}

A test case with x = 0 is missing. What branch coverage issue does this cause?
medium
A. True branch not tested
B. False branch not tested
C. Syntax error in code
D. All branches covered

Solution

  1. Step 1: Identify branches in the if condition

    The if condition has two branches: true (x != 0) and false (x == 0).
  2. Step 2: Analyze test cases given

    Only test cases with x != 0 exist, so true branch is tested but false branch (x == 0) is missing.
  3. Final Answer:

    False branch not tested -> Option B
  4. Quick Check:

    Missing false branch test = coverage gap [OK]
Hint: Test both true and false conditions for full coverage [OK]
Common Mistakes:
  • Assuming one test case covers all branches
  • Confusing true and false branches
  • Thinking missing test causes syntax error
5. Given this code:
if (score >= 90) {
  grade = 'A';
} else if (score >= 75) {
  grade = 'B';
} else {
  grade = 'C';
}

How many test cases are needed for 100% branch coverage?
hard
A. 2
B. 5
C. 3
D. 4

Solution

  1. Step 1: Identify all decision points and branches

    There are two if conditions: first (score >= 90) with true/false branches, second (score >= 75) with true/false branches inside else.
  2. Step 2: Count unique branches

    Branches: first if true (score>=90), first false + second true (75<=score<90), first false + second false (score<75). Total 3 test cases needed for 100% coverage.
  3. Final Answer:

    3 -> Option C
  4. Quick Check:

    if-else-if-else chain = 3 test cases [OK]
Hint: Count all true/false branches in if-else if-else chains [OK]
Common Mistakes:
  • Counting only if statements, ignoring else
  • Assuming 4 test cases are needed
  • Missing nested else if branches
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard