Verify white-box testing inspects code internals
Preconditions (2)
✅ Expected Result: The test should confirm that all internal branches of the function are tested and behave as expected
import unittest # Function to test def calculate_discount(customer_type, amount): if customer_type == 'regular': return amount * 0.9 # 10% discount elif customer_type == 'vip': return amount * 0.8 # 20% discount else: return amount # no discount class TestCalculateDiscount(unittest.TestCase): def test_regular_customer(self): self.assertEqual(calculate_discount('regular', 100), 90) def test_vip_customer(self): self.assertEqual(calculate_discount('vip', 100), 80) def test_unknown_customer(self): self.assertEqual(calculate_discount('guest', 100), 100) if __name__ == '__main__': unittest.main()
This test script uses Python's unittest framework to check the calculate_discount function.
Each test method checks a different internal branch of the function: 'regular', 'vip', and unknown customer types.
Assertions verify the output matches expected discounts, ensuring the internal logic is tested.
This approach shows white-box testing by directly examining and validating the code's internal decision paths.
Now add data-driven testing with 3 different customer types and amounts
def is_even(n):
if n % 2 == 0:
return True
else:
return False
# Test case
assert is_even(3) == False
assert is_even(4) == True
What is the result of running this test case?def calculate_discount(price):
if price > 100:
return price * 0.9
else:
return price * 0.95
# Test
assert calculate_discount(100) == 90What is the problem with this test?def classify_number(x):
if x > 0:
if x % 2 == 0:
return 'Positive even'
else:
return 'Positive odd'
elif x < 0:
return 'Negative'
else:
return 'Zero'Which set of test inputs covers all branches?Pass the test and this pattern moves unknown