AudioForge / RUN_TESTS.md
OnyxlMunkey's picture
c618549

Running Tests - Quick Reference

πŸš€ Quick Start

Backend Tests (Python/Pytest)

cd backend
.venv\Scripts\activate
pytest

Frontend Tests (TypeScript/Vitest)

cd frontend
pnpm test

πŸ“Š Backend Tests (Pytest)

Run All Tests

cd backend
pytest

Run with Coverage Report

pytest --cov=app --cov-report=html --cov-report=term-missing

Run Specific Test File

# Music generation tests
pytest tests/test_music_generation.py -v

# Post-processing tests
pytest tests/test_post_processing.py -v

# Vocal generation tests
pytest tests/test_vocal_generation.py -v

# Database model tests
pytest tests/test_models.py -v

Run Specific Test Class

pytest tests/test_music_generation.py::TestMusicGenerationServiceInitialization -v

Run Specific Test Method

pytest tests/test_music_generation.py::TestMusicGenerationServiceInitialization::test_service_initializes_without_ml_dependencies -v

Run Tests with Markers

# Run only unit tests
pytest -m unit

# Run only integration tests
pytest -m integration

# Skip slow tests
pytest -m "not slow"

Run Tests in Parallel

# Install pytest-xdist first
pip install pytest-xdist

# Run with 4 workers
pytest -n 4

View Coverage Report

# Generate HTML report
pytest --cov=app --cov-report=html

# Open in browser (Windows)
start htmlcov/index.html

🎨 Frontend Tests (Vitest)

Run All Tests

cd frontend
pnpm test

Run with Coverage

pnpm test --coverage

Run in Watch Mode

pnpm test --watch

Run Specific Test File

# useToast hook tests
pnpm test use-toast.test.ts

# Providers component tests
pnpm test providers.test.tsx

Run Tests with UI

pnpm test:ui

Run Tests Matching Pattern

# Run tests with "toast" in the name
pnpm test --grep="toast"

# Run tests with "error" in the name
pnpm test --grep="error"

πŸ” Debugging Tests

Backend - Debug with Print Statements

pytest tests/test_music_generation.py -s

Backend - Debug with PDB

# Add to test
import pdb; pdb.set_trace()
pytest tests/test_music_generation.py --pdb

Backend - Show Full Traceback

pytest --tb=long

Frontend - Debug in Browser

pnpm test:ui
# Opens browser with test UI

πŸ“ˆ Coverage Goals

Current Coverage

  • Overall: 95.8%
  • Target: β‰₯92%
  • Status: βœ… Exceeding target

Check Coverage by File

# Backend
cd backend
pytest --cov=app --cov-report=term-missing

# Frontend
cd frontend
pnpm test --coverage

Coverage Thresholds

# Backend - Fail if coverage < 92%
pytest --cov=app --cov-fail-under=92

# Frontend - Configure in vitest.config.ts

πŸ§ͺ Test Types

Unit Tests

Test individual functions/methods in isolation

pytest -m unit

Integration Tests

Test multiple components working together

pytest -m integration

Async Tests

Tests using async/await

pytest -m asyncio

🎯 Common Test Scenarios

Test New Feature

# 1. Write test first (TDD)
# 2. Run test (should fail)
pytest tests/test_new_feature.py -v

# 3. Implement feature
# 4. Run test again (should pass)
pytest tests/test_new_feature.py -v

Test Bug Fix

# 1. Write test that reproduces bug
# 2. Verify test fails
pytest tests/test_bug_fix.py -v

# 3. Fix bug
# 4. Verify test passes
pytest tests/test_bug_fix.py -v

Test Refactoring

# 1. Run all tests before refactoring
pytest

# 2. Refactor code
# 3. Run all tests again
pytest

# 4. Verify coverage didn't decrease
pytest --cov=app

🚨 Troubleshooting

Backend Tests Failing

Issue: Import errors

# Solution: Ensure in backend directory and venv activated
cd backend
.venv\Scripts\activate
pytest

Issue: Missing dependencies

# Solution: Install test dependencies
pip install -e ".[dev]"

Issue: Database connection errors

# Solution: Tests should use mocks, not real DB
# Check test file has proper mocking

Frontend Tests Failing

Issue: Module not found

# Solution: Install dependencies
pnpm install

Issue: Tests timing out

# Solution: Increase timeout
pnpm test --testTimeout=10000

Issue: React hooks errors

# Solution: Ensure using @testing-library/react
# Check renderHook is imported correctly

πŸ“ Test Output Examples

Successful Test Run

============================= test session starts ==============================
collected 133 items

tests/test_music_generation.py ......................          [ 16%]
tests/test_post_processing.py ......................           [ 33%]
tests/test_vocal_generation.py ...............               [ 44%]
tests/test_models.py ................................        [100%]

============================== 133 passed in 5.23s ==============================

Coverage Report

Name                                      Stmts   Miss Branch BrPart  Cover
---------------------------------------------------------------------------
app/services/music_generation.py            145      8     42      3    94%
app/services/post_processing.py             98      5     28      2    95%
app/services/vocal_generation.py            76      5     20      2    93%
app/db/models.py                            45      1      8      0    98%
---------------------------------------------------------------------------
TOTAL                                       364     19     98      7    95.8%

πŸ”„ Continuous Integration

Pre-commit Hook

# .git/hooks/pre-commit
#!/bin/sh
cd backend && pytest --cov=app --cov-fail-under=92
cd ../frontend && pnpm test

GitHub Actions

See .github/workflows/tests.yml for CI configuration

πŸ“š Additional Resources


Quick Commands Summary

# Backend - All tests with coverage
cd backend && pytest --cov=app --cov-report=html

# Frontend - All tests with coverage
cd frontend && pnpm test --coverage

# Backend - Watch mode (requires pytest-watch)
cd backend && ptw

# Frontend - Watch mode
cd frontend && pnpm test --watch

# Both - Run all tests
cd backend && pytest && cd ../frontend && pnpm test