Test Overview
This test checks a simple calculator's behavior using decision table testing. It verifies that the calculator correctly adds or subtracts two numbers based on the selected operation.
This test checks a simple calculator's behavior using decision table testing. It verifies that the calculator correctly adds or subtracts two numbers based on the selected operation.
import unittest class Calculator: def calculate(self, num1, num2, operation): if operation == 'add': return num1 + num2 elif operation == 'subtract': return num1 - num2 else: raise ValueError('Invalid operation') class TestCalculator(unittest.TestCase): def setUp(self): self.calc = Calculator() def test_addition(self): result = self.calc.calculate(5, 3, 'add') self.assertEqual(result, 8) def test_subtraction(self): result = self.calc.calculate(5, 3, 'subtract') self.assertEqual(result, 2) def test_invalid_operation(self): with self.assertRaises(ValueError): self.calc.calculate(5, 3, 'multiply') if __name__ == '__main__': unittest.main()
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts - unittest framework initializes TestCalculator | Test environment ready, Calculator instance created | - | PASS |
| 2 | Calls calculate(5, 3, 'add') | Calculator receives inputs num1=5, num2=3, operation='add' | - | PASS |
| 3 | Calculator performs addition and returns 8 | Result = 8 | Check if result == 8 | PASS |
| 4 | Calls calculate(5, 3, 'subtract') | Calculator receives inputs num1=5, num2=3, operation='subtract' | - | PASS |
| 5 | Calculator performs subtraction and returns 2 | Result = 2 | Check if result == 2 | PASS |
| 6 | Calls calculate(5, 3, 'multiply') expecting error | Calculator receives inputs num1=5, num2=3, operation='multiply' | Check if ValueError is raised | PASS |
| 7 | ValueError raised due to invalid operation | Exception caught by test | Exception matches expected ValueError | PASS |
| 8 | All tests complete | Test report generated with 3 tests passed | - | PASS |