0
0
Testing Fundamentalstesting~15 mins

Why different testing levels catch different bugs in Testing Fundamentals - Automation Benefits in Action

Choose your learning style9 modes available
Verify bug detection at different testing levels
Preconditions (2)
Step 1: Run unit tests on each calculator function separately
Step 2: Run integration tests on combined operations (e.g., multiply then divide)
Step 3: Run system tests on the full calculator UI by performing operations
Step 4: Observe which bugs are detected at each testing level
✅ Expected Result: Unit tests catch the bug in the add function; integration tests catch the bug in combined multiply/divide operations; system tests catch the UI display bug
Automation Requirements - pytest
Assertions Needed:
Assert that unit tests fail for add function bug
Assert that integration tests fail for multiply/divide bug
Assert that system tests fail for UI display bug
Best Practices:
Use clear test function names indicating testing level
Isolate unit tests from UI and integration
Use fixtures to set up calculator instances
Use pytest markers to separate test levels
Automated Solution
Testing Fundamentals
import pytest

class Calculator:
    def add(self, a, b):
        # Bug: returns wrong sum intentionally
        return a + b + 1

    def subtract(self, a, b):
        return a - b

    def multiply(self, a, b):
        return a * b

    def divide(self, a, b):
        if b == 0:
            raise ValueError("Cannot divide by zero")
        return a / b

# Unit tests
@pytest.mark.unit
def test_add_bug():
    calc = Calculator()
    result = calc.add(2, 3)
    assert result == 5, f"Expected 5 but got {result}"

@pytest.mark.unit
def test_subtract():
    calc = Calculator()
    assert calc.subtract(5, 3) == 2

# Integration test
@pytest.mark.integration
def test_multiply_divide_bug():
    calc = Calculator()
    mul = calc.multiply(4, 5)  # 20
    # Bug: divide returns wrong result if multiply used before
    # Simulate bug by altering divide to return wrong value if mul was called
    # For demo, we simulate bug by forcing wrong divide result
    divide_result = mul / 0  # This will raise ZeroDivisionError to simulate bug
    assert divide_result == 4, f"Expected 4 but got {divide_result}"

# System test
@pytest.mark.system
def test_ui_display_bug():
    # Simulate UI display bug by checking displayed result string
    displayed_result = "Result is 9"  # Bug: should be 8
    expected_display = "Result is 8"
    assert displayed_result == expected_display, f"UI display bug: expected '{expected_display}' but got '{displayed_result}'"

This test script uses pytest to automate testing at different levels.

The Calculator class has a bug in the add method (returns sum + 1).

Unit tests check individual functions like add and subtract. The test_add_bug test will fail because of the bug.

Integration test test_multiply_divide_bug simulates a bug when combining multiply and divide. It intentionally causes a division by zero error to represent a bug caught at integration level.

System test test_ui_display_bug simulates a UI display bug by comparing expected and actual displayed strings.

Using @pytest.mark decorators helps separate tests by level for clarity and selective running.

Common Mistakes - 3 Pitfalls
Not isolating unit tests from integration or system tests
Hardcoding expected results without considering known bugs
{'mistake': 'Not simulating realistic bugs at each level', 'why_bad': "Tests won't show the value of different testing levels in catching bugs.", 'correct_approach': 'Create simple but clear bugs in code or test data that each level can catch.'}
Bonus Challenge

Now add data-driven testing with 3 different input sets for the add function to show the bug consistently.

Show Hint