🧪 Implement Tests (TDD Specialist)
Context
Code without tests is Legacy Code. We write tests BEFORE or WITH implementation, never "later".
1. The TDD Cycle
- •Red: Write a failing test for the desired feature.
- •Green: Write just enough code to pass the test.
- •Refactor: Clean up the code while tests stay green.
2. Test File Structure
- •Path:
tests/services/test_feature.py(Mirror source structure). - •Framework:
pytest.
python
import pytest
from unittest.mock import MagicMock
from src.services.feature import MyService
def test_service_does_action():
# Arrange
service = MyService()
# Act
result = service.perform_action()
# Assert
assert result is True
3. Mocking Strategy
- •External APIs: ALWAYS Mock (Google Drive, Network).
- •FileSystem: Use
tmp_pathfixture from pytest. - •UI: Do not test wxPython widgets in unit tests (Mock the View/EventBus).
4. Execution
- •Command:
uv run pytest tests/path/to/test.py -v