In Test-Driven Development (TDD), you write tests before you write the implementation. This lab walks you through building a Calculator class using TDD.
Your Task
Implement the Calculator class and its test suite in the same file.
The Calculator class must have:
add(a, b) โ returns a + b
subtract(a, b) โ returns a - b
multiply(a, b) โ returns a * b
divide(a, b) โ returns a / b, raises ZeroDivisionError if b == 0
power(base, exp) โ returns base ** exp
history โ a list of strings recording each operation, e.g. "3 + 4 = 7"
clear_history() โ empties the history list
The TestCalculator class must have at least these test methods:
test_add โ tests addition
test_subtract โ tests subtraction
test_multiply โ tests multiplication
test_divide โ tests normal division
test_divide_by_zero โ tests that divide raises ZeroDivisionError
test_power โ tests exponentiation
test_history_records_operations โ tests that operations are logged in history
test_clear_history โ tests that clear_history() empties history
Running tests
At the bottom of the file, add:
This routes the test output to stdout so it appears in the output panel.