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.