0
0
Testing Fundamentalstesting~15 mins

Why testing prevents costly failures in Testing Fundamentals - Automation Benefits in Action

Choose your learning style9 modes available
Verify that a function correctly calculates the total price including tax
Preconditions (3)
Step 1: Call calculate_total_price with price=100.0 and tax_rate=0.1
Step 2: Call calculate_total_price with price=50.0 and tax_rate=0.2
Step 3: Call calculate_total_price with price=0 and tax_rate=0.15
✅ Expected Result: The function returns 110.0, 60.0, and 0.0 respectively for the three calls
Automation Requirements - pytest
Assertions Needed:
Assert that calculate_total_price(100.0, 0.1) == 110.0
Assert that calculate_total_price(50.0, 0.2) == 60.0
Assert that calculate_total_price(0, 0.15) == 0.0
Best Practices:
Use clear and descriptive test function names
Use assert statements for validation
Keep tests independent and simple
Test edge cases such as zero price
Automated Solution
Testing Fundamentals
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.

Common Mistakes - 3 Pitfalls
Not using assert statements to verify output
Testing only one input case
Hardcoding expected results incorrectly
Bonus Challenge

Now add data-driven testing with 3 different sets of price and tax_rate inputs

Show Hint