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.
Jump into concepts and practice - no test required
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 |
add and multiply are used together in one test.def test_process_order():
order = create_order(5)
result = process_payment(order)
assert result == 'Success'create_order and then process_payment with the order. It asserts the result equals 'Success'.process_payment(order) returns 'Success', the assertion passes and the test passes. Otherwise, it fails.def test_user_login():
user = create_user('alice')
assert login(user) == True
assert logout(user) = Truefetch_data() returns data list, and process_data(data) filters it. Why is an integration test combining both important?fetch_data() gets data, process_data(data) filters it. Testing them together checks real interaction.process_data handles actual data from fetch_data, catching issues missed by isolated tests.