Verify that a function correctly calculates the total price including tax
Preconditions (3)
✅ Expected Result: The function returns 110.0, 60.0, and 0.0 respectively for the three calls
import pytest def calculate_total_price(price: float, tax_rate: float) -> float: return price + (price * tax_rate) def test_calculate_total_price(): assert calculate_total_price(100.0, 0.1) == 110.0 assert calculate_total_price(50.0, 0.2) == 60.0 assert calculate_total_price(0, 0.15) == 0.0 if __name__ == "__main__": pytest.main()
The calculate_total_price function is defined to add tax to the price.
The test function test_calculate_total_price calls this function with three sets of inputs and uses assert statements to check if the output matches the expected values.
This simple test prevents costly failures by ensuring the calculation logic is correct before using it in real scenarios.
Using pytest allows easy running and reporting of test results.
Now add data-driven testing with 3 different sets of price and tax_rate inputs
def test_sum():
result = sum([1, 2, 3])
assert result == 6
print('Test passed')
What will be the output when this test runs?def test_divide():
result = 10 / 0
assert result == 0
What is the main problem with this test?Pass the test and this pattern moves unknown