0
0
Testing Fundamentalstesting~15 mins

Cost of bugs at different stages in Testing Fundamentals - Build an Automation Script

Choose your learning style9 modes available
Verify cost calculation of bugs at different development stages
Preconditions (2)
Step 1: Call calculate_bug_cost('requirements')
Step 2: Call calculate_bug_cost('design')
Step 3: Call calculate_bug_cost('development')
Step 4: Call calculate_bug_cost('testing')
Step 5: Call calculate_bug_cost('production')
Step 6: Verify the returned cost matches expected values for each stage
✅ Expected Result: The function returns correct costs: requirements=1, design=5, development=10, testing=20, production=100
Automation Requirements - pytest
Assertions Needed:
Assert returned cost equals expected cost for each stage
Best Practices:
Use parameterized tests to cover all stages
Use clear assertion messages
Keep test code simple and readable
Automated Solution
Testing Fundamentals
import pytest

def calculate_bug_cost(stage: str) -> int:
    costs = {
        'requirements': 1,
        'design': 5,
        'development': 10,
        'testing': 20,
        'production': 100
    }
    return costs.get(stage, 0)

@pytest.mark.parametrize('stage,expected_cost', [
    ('requirements', 1),
    ('design', 5),
    ('development', 10),
    ('testing', 20),
    ('production', 100)
])
def test_calculate_bug_cost(stage, expected_cost):
    cost = calculate_bug_cost(stage)
    assert cost == expected_cost, f"Cost for stage '{stage}' should be {expected_cost} but got {cost}"

This test uses pytest with parameterization to check the bug cost at each stage.

The calculate_bug_cost function returns the cost based on a dictionary lookup.

The test test_calculate_bug_cost runs once for each stage and asserts the returned cost matches the expected value.

Assertions include messages to help identify failures clearly.

Common Mistakes - 3 Pitfalls
Hardcoding test cases without parameterization
Not checking for invalid stages
Using print statements instead of assertions
Bonus Challenge

Now add data-driven testing with 3 additional invalid stage inputs and verify the cost is zero

Show Hint