Bird
Raised Fist0
Testing Fundamentalstesting~15 mins

Path coverage in Testing Fundamentals - Build an Automation Script

Choose your learning style10 modes available
Test path coverage for a simple function with multiple branches
Preconditions (2)
Step 1: Call calculate_discount with price=100 and is_member=True
Step 2: Verify the returned value is 90
Step 3: Call calculate_discount with price=100 and is_member=False
Step 4: Verify the returned value is 100
Step 5: Call calculate_discount with price=50 and is_member=True
Step 6: Verify the returned value is 50
Step 7: Call calculate_discount with price=50 and is_member=False
Step 8: Verify the returned value is 50
✅ Expected Result: All calls return the correct discounted price according to the function logic, covering all possible paths
Automation Requirements - unittest
Assertions Needed:
assertEqual to verify returned discounted price matches expected value
Best Practices:
Use clear test method names describing the path tested
Test all possible paths through the function
Keep tests independent and repeatable
Automated Solution
Testing Fundamentals
import unittest

# Function to test

def calculate_discount(price: int, is_member: bool) -> int:
    if is_member:
        if price > 75:
            return int(price * 0.9)  # 10% discount
        else:
            return price
    else:
        return price

class TestCalculateDiscount(unittest.TestCase):
    def test_member_price_above_75(self):
        result = calculate_discount(100, True)
        self.assertEqual(result, 90)

    def test_non_member_price_above_75(self):
        result = calculate_discount(100, False)
        self.assertEqual(result, 100)

    def test_member_price_50(self):
        result = calculate_discount(50, True)
        self.assertEqual(result, 50)

    def test_non_member_price_50(self):
        result = calculate_discount(50, False)
        self.assertEqual(result, 50)

if __name__ == '__main__':
    unittest.main()

The calculate_discount function has two decision points: whether the user is a member and whether the price is above 75. This creates four possible paths.

The test class TestCalculateDiscount has four test methods, each covering one path:

  • test_member_price_above_75: member with price 100, expects 10% discount
  • test_non_member_price_above_75: non-member with price 100, no discount
  • test_member_price_50: member with price 50, no discount
  • test_non_member_price_50: non-member with price 50, no discount

Each test calls the function with specific inputs and asserts the output matches the expected discounted price. This ensures full path coverage.

Using unittest framework provides clear structure and assertion methods. The tests are independent and repeatable.

Common Mistakes - 4 Pitfalls
Testing only one path and missing other branches
Using print statements instead of assertions
Hardcoding expected values without understanding logic
Not isolating tests, causing dependencies between them
Bonus Challenge

Now add data-driven testing with 3 different input sets to cover all paths

Show Hint

Practice

(1/5)
1. What does path coverage testing ensure in software testing?
easy
A. All possible routes through the code are tested
B. Only the most common routes are tested
C. Only the start and end points are tested
D. Only the user interface is tested

Solution

  1. Step 1: Understand path coverage meaning

    Path coverage means testing every possible route or path through the code's decision points.
  2. Step 2: Compare options with definition

    All possible routes through the code are tested matches this definition exactly, while others limit testing scope incorrectly.
  3. Final Answer:

    All possible routes through the code are tested -> Option A
  4. Quick Check:

    Path coverage = all routes tested [OK]
Hint: Path coverage means testing every route through code [OK]
Common Mistakes:
  • Confusing path coverage with statement coverage
  • Thinking it tests only common or simple paths
  • Assuming it tests only UI or start/end points
2. Which of the following is the correct way to represent path coverage in a decision structure?
easy
A. Testing only the true branch of an if statement
B. Testing only the false branch of an if statement
C. Testing both true and false branches of an if statement
D. Skipping the if statement entirely

Solution

  1. Step 1: Understand path coverage in decisions

    Path coverage requires testing all possible outcomes of decisions, including both true and false branches.
  2. Step 2: Match options to requirement

    Only Testing both true and false branches of an if statement tests both branches, fulfilling path coverage requirements.
  3. Final Answer:

    Testing both true and false branches of an if statement -> Option C
  4. Quick Check:

    Both branches tested = path coverage [OK]
Hint: Test all branches of decisions for path coverage [OK]
Common Mistakes:
  • Testing only one branch of a decision
  • Ignoring false branches
  • Skipping decision points
3. Consider this code snippet:
if (x > 0) {
  if (x < 10) {
    print("A");
  } else {
    print("B");
  }
} else {
  print("C");
}

How many unique paths must be tested for full path coverage?
medium
A. 2
B. 3
C. 4
D. 5

Solution

  1. Step 1: Identify decision points and paths

    There are two if statements: outer (x > 0) and inner (x < 10). Paths are:
    1) x > 0 and x < 10 -> print "A"
    2) x > 0 and x >= 10 -> print "B"
    3) x <= 0 -> print "C"
  2. Step 2: Count unique paths

    There are 3 unique paths through the code based on these conditions.
  3. Final Answer:

    3 -> Option B
  4. Quick Check:

    Two decisions create 3 paths [OK]
Hint: Count all decision outcomes to find paths [OK]
Common Mistakes:
  • Counting only outer if branches
  • Assuming paths multiply incorrectly
  • Forgetting else branch path
4. You have this code:
if (a > 5) {
  if (b == 0) {
    print("X");
  }
} else {
  print("Y");
}

Which test case is missing for full path coverage?
medium
A. a = 6, b = 1
B. a = 6, b = 0
C. a = 4, b = 0
D. a = 4, b = 1

Solution

  1. Step 1: Identify all paths

    Paths are:
    1) a > 5 and b == 0 -> print "X"
    2) a > 5 and b != 0 -> no print
    3) a <= 5 -> print "Y"
  2. Step 2: Check test cases for coverage

    The test case a = 6, b = 1 covers path 2, which is missing from the others.
  3. Final Answer:

    a = 6, b = 1 -> Option A
  4. Quick Check:

    Test all branches including no-action paths [OK]
Hint: Include tests for conditions with no output too [OK]
Common Mistakes:
  • Ignoring paths with no print or action
  • Testing only true branches
  • Missing else branch tests
5. Given this nested decision code:
if (x > 0) {
  if (y > 0) {
    print("P");
  } else {
    print("Q");
  }
} else {
  if (y > 0) {
    print("R");
  } else {
    print("S");
  }
}

How many test cases are needed for full path coverage, and why?
hard
A. 2, because only outer if matters
B. 5, because of extra hidden paths
C. 3, because some inner paths overlap
D. 4, because each if-else pair creates 2 paths

Solution

  1. Step 1: Analyze nested if-else structure

    There are two if-else blocks nested:
    - Outer if (x > 0) with two inner paths (y > 0 and y <= 0)
    - Outer else (x <= 0) with two inner paths (y > 0 and y <= 0)
  2. Step 2: Calculate total paths

    Each outer branch has 2 inner branches, so total paths = 2 (outer) * 2 (inner) = 4.
  3. Final Answer:

    4, because each if-else pair creates 2 paths -> Option D
  4. Quick Check:

    Nested if-else multiply paths = 4 [OK]
Hint: Multiply branches of nested decisions for paths count [OK]
Common Mistakes:
  • Counting only outer or inner branches
  • Assuming overlapping paths reduce count
  • Adding extra paths without basis
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