Verify calculator operations using pytest test class
Preconditions (2)
✅ Expected Result: All test methods pass confirming Calculator operations work correctly
import pytest class Calculator: def add(self, a, b): return a + b 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 class TestCalculator: def setup_method(self): self.calc = Calculator() def test_add(self): result = self.calc.add(2, 3) assert result == 5 def test_subtract(self): result = self.calc.subtract(10, 5) assert result == 5 def test_multiply(self): result = self.calc.multiply(4, 3) assert result == 12 def test_divide(self): result = self.calc.divide(10, 2) assert result == 5
The Calculator class provides basic math operations.
The TestCalculator class groups tests for these operations.
The setup_method runs before each test to create a fresh Calculator instance.
Each test method calls one Calculator method and asserts the result matches the expected value.
This structure keeps tests organized and avoids repeating setup code.
Now add data-driven testing with 3 different input sets for the add method
class TestMath:
def test_add(self):
assert 1 + 1 == 2
def test_subtract(self):
assert 5 - 3 == 1
class TestCalc:
def test_multiply(self):
assert 2 * 3 == 6
def multiply_test(self):
assert 4 * 5 == 20
Pass the test and this pattern moves unknown