0
0
Testing Fundamentalstesting~15 mins

Path coverage in Testing Fundamentals - Build an Automation Script

Choose your learning style9 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