Spaces:
Sleeping
Sleeping
File size: 5,820 Bytes
d00203b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 | # VoiceForge Testing Guide
## Overview
VoiceForge uses a comprehensive, multi-layered testing strategy to ensure code quality, security, and performance.
## Test Structure
```
backend/tests/
βββ unit/ # Service-level unit tests
β βββ test_stt_service.py
β βββ test_tts_service.py
β βββ test_translation_service.py
β βββ test_emotion_meeting_service.py
β βββ test_audio.py
β βββ test_cloning.py
β βββ test_export.py
β βββ test_nlp.py
β βββ test_sign.py
βββ integration/ # API & E2E tests
β βββ test_auth.py
β βββ test_api_integration.py
β βββ test_e2e_full_flow.py
β βββ test_diarization.py
β βββ test_project_health.py
βββ performance/ # Load & benchmark tests
β βββ locustfile.py
β βββ benchmark_*.py
β βββ run_benchmarks.py
βββ quality/ # Code quality tools
β βββ analyze_codebase.py
β βββ check_syntax.py
β βββ check_dependencies.py
β βββ check_pipeline.py
β βββ coverage_tracker.py
β βββ lighthouse_audit.py
β βββ project_audit.py
βββ security/ # Security audits
β βββ run_audit.py
βββ conftest.py # Shared fixtures
βββ pytest.ini # Pytest configuration
βββ run_all_tests.py # Master test runner
```
## Running Tests
### Quick Start
```bash
cd backend
python tests/run_all_tests.py
```
### Individual Test Categories
```bash
# Unit tests
pytest tests/unit -v
# Integration tests
pytest tests/integration -v
# Performance benchmarks
python tests/performance/run_benchmarks.py
# Security audit
python tests/security/run_audit.py
```
### Docker (Recommended)
```bash
# Run all tests in clean Docker environment
docker-compose run --rm backend python tests/run_all_tests.py
# Run specific category
docker-compose run --rm backend pytest tests/unit -v
```
## Quality Tools
### Code Quality Analysis
```bash
python tests/quality/analyze_codebase.py --path app
```
**Checks:**
- File sizes (flags >500 lines)
- Cyclomatic complexity (Radon)
- Maintainability index
- Long functions (>50 lines)
- Import dependencies
### Syntax & Import Check
```bash
python tests/quality/check_syntax.py --path app
```
**Checks:**
- Python syntax errors
- Circular imports
- Missing `__init__.py` files
### Dependency Health
```bash
python tests/quality/check_dependencies.py
```
**Checks:**
- Local pip compatibility (`pip check`)
- PyPI availability (online check)
- Outdated packages
- Security vulnerabilities (via `pip-audit`)
### Pipeline Validation
```bash
python tests/quality/check_pipeline.py --root ..
```
**Checks:**
- GitHub Actions workflow syntax
- Dockerfile validation
- docker-compose.yml structure
- Environment file presence
### Coverage Tracking
```bash
python tests/quality/coverage_tracker.py --app app --tests tests
```
**Provides:**
- Module coverage matrix
- Untested function identification
- Coverage percentage by component
### Frontend Performance (Lighthouse)
```bash
python tests/quality/lighthouse_audit.py --url http://localhost:8501
```
**Audits:**
- Performance score
- Accessibility score
- Best practices
- SEO metrics
## Test Fixtures
### Database Setup
```python
@pytest.fixture(scope="module")
def setup_db():
Base.metadata.create_all(bind=engine)
yield
# Cleanup optional
```
### Test Client (Sync)
```python
@pytest.fixture
def client():
return TestClient(app)
```
### Async Client
```python
@pytest_asyncio.fixture
async def async_client():
transport = ASGITransport(app=app)
async with AsyncClient(transport=transport, base_url="http://test") as client:
yield client
```
## Best Practices
### Writing Unit Tests
- Test one function per test case
- Use mocks for external dependencies
- Follow AAA pattern (Arrange, Act, Assert)
```python
@pytest.mark.asyncio
async def test_synthesize_returns_audio_bytes():
# Arrange
with patch('app.services.edge_tts_service.EdgeTTSService') as MockService:
mock_service = MockService.return_value
mock_service.synthesize = AsyncMock(return_value=b'AUDIO_DATA')
# Act
audio = await mock_service.synthesize("Hello", voice="en-US-Neural2-F")
# Assert
assert isinstance(audio, bytes)
assert len(audio) > 0
```
### Writing Integration Tests
- Test realistic user flows
- Use actual database (with cleanup)
- Test authentication & authorization
```python
def test_full_user_journey(client):
# Register
response = client.post("/api/v1/auth/register", json={...})
assert response.status_code == 200
# Login
response = client.post("/api/v1/auth/login", data={...})
token = response.json()["access_token"]
# Use protected endpoint
response = client.get("/api/v1/auth/me", headers={"Authorization": f"Bearer {token}"})
assert response.status_code == 200
```
## CI/CD Integration
### GitHub Actions Workflow
```yaml
name: Test Suite
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Run tests
run: |
docker-compose run --rm backend python tests/run_all_tests.py
```
## Coverage Goals
- **Unit Tests**: >80% line coverage
- **Integration Tests**: All critical user flows
- **Performance**: All endpoints benchmarked
- **Security**: Zero high/critical vulnerabilities
## Current Status
β
**74+ tests** across all categories
β
**100% module coverage** (all services have tests)
β
**7 quality tools** for automated analysis
β
**Master runner** for one-command execution
|