Test Overview
This test checks that two components, a calculator and a logger, work correctly when used together. It verifies that the calculator adds numbers and the logger records the operation.
This test checks that two components, a calculator and a logger, work correctly when used together. It verifies that the calculator adds numbers and the logger records the operation.
import pytest class Calculator: def __init__(self, logger): self.logger = logger def add(self, a, b): result = a + b self.logger.log(f"Added {a} and {b} to get {result}") return result class Logger: def __init__(self): self.messages = [] def log(self, message): self.messages.append(message) @pytest.fixture def logger(): return Logger() @pytest.fixture def calculator(logger): return Calculator(logger) def test_calculator_add_logs_operation(calculator, logger): result = calculator.add(3, 4) assert result == 7 assert logger.messages[-1] == "Added 3 and 4 to get 7"
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts | pytest test runner initialized | - | PASS |
| 2 | Fixtures create Logger and Calculator instances | Logger with empty messages list, Calculator linked to Logger | - | PASS |
| 3 | Calculator.add(3, 4) is called | Calculator computes sum 7 | - | PASS |
| 4 | Logger.log records message 'Added 3 and 4 to get 7' | Logger messages list contains one entry | Logger messages list last entry is 'Added 3 and 4 to get 7' | PASS |
| 5 | Assert result == 7 | Result is 7 | 7 == 7 | PASS |
| 6 | Assert logger message is correct | Logger messages list last entry is 'Added 3 and 4 to get 7' | Message matches expected string | PASS |
| 7 | Test ends | All assertions passed | - | PASS |