Testing
Testing guidelines and structure for Copinance OS.
Test Structure
Tests are organized to mirror the source code structure:
tests/
├── unit/ # Unit tests
│ └── copinanceos/
│ ├── domain/
│ ├── application/
│ └── infrastructure/
└── integration/ # Integration tests
└── copinanceos/
└── infrastructure/Running Tests
# All tests
pytest
# Unit tests only
pytest -m unit
# Integration tests only
pytest -m integration
# With coverage
pytest --cov=copinanceos --cov-report=htmlWriting Tests
Unit Tests
Test individual components in isolation:
import pytest
from copinanceos.domain.models.research import Research, ResearchStatus
def test_research_creation():
research = Research(
stock_symbol="AAPL",
timeframe=ResearchTimeframe.MID_TERM,
workflow_type="static"
)
assert research.status == ResearchStatus.PENDINGIntegration Tests
Test component interactions:
import pytest
from copinanceos.infrastructure.repositories import ResearchRepositoryImpl
@pytest.mark.integration
async def test_research_repository_save_and_get(isolated_storage):
repo = ResearchRepositoryImpl(storage=isolated_storage)
research = Research(stock_symbol="AAPL", ...)
saved = await repo.save(research)
retrieved = await repo.get_by_id(saved.id)
assert retrieved is not None
assert retrieved.id == saved.idTest Fixtures
Common fixtures are available in tests/conftest.py:
isolated_storage: Isolated storage for each testprofile_repository: Profile repository with isolated storageresearch_repository: Research repository with isolated storage
Test Markers
Use markers to categorize tests:
@pytest.mark.unit
def test_unit_example():
pass
@pytest.mark.integration
async def test_integration_example():
passBest Practices
- Isolation: Each test should be independent
- Fixtures: Use fixtures for common setup
- Async: Use
pytest-asynciofor async tests - Coverage: Aim for high test coverage
- Naming: Use descriptive test names