๐งช Testing Guide
This document outlines the testing strategy and organization for the Vertical Farm project.
๐ Test Organization
We use a hybrid test organization that balances maintainability with CI/CD efficiency:
vertical-farm/
โโโ backend/app/tests/ # Backend unit & integration tests
โโโ frontend/tests/ # Frontend unit & integration tests
โโโ tests/ # Cross-cutting tests (E2E, performance)
โโโ .github/workflows/tests.yml # Unified CI/CD workflow
โโโ test-all.sh # Local test runner
๐ Backend Tests (backend/app/tests/
)
Current Status: โ Well-organized and functional
backend/app/tests/
โโโ conftest.py # Shared fixtures and mocks
โโโ test_main.py # Main application tests
โโโ test_cache_endpoints.py # Cache API tests
โโโ test_home_assistant_endpoints.py # Home Assistant API tests
โโโ unit/ # Unit tests (recommended organization)
โโโ integration/ # Integration tests
โโโ __pycache__/ # Python cache (auto-generated)
Framework: pytest with async support Coverage: 26+ tests covering critical endpoints Features: - Proper async fixtures - Supabase mocking - Authentication testing - Health check validation
โ๏ธ Frontend Tests (frontend/tests/
)
Current Status: โ ๏ธ Minimal coverage - needs expansion
frontend/tests/
โโโ App.test.js # Basic app test
โโโ unit/
โ โโโ grow-recipe-utils.test.ts # Utility tests
โโโ component/ # Component tests (empty)
โโโ e2e/ # E2E tests (empty)
โโโ integration/ # Integration tests (empty)
Framework: Jest + React Testing Library Coverage: Very limited Needs: Comprehensive component and integration tests
๐ Cross-Cutting Tests (tests/
)
Current Status: โ Comprehensive and well-documented
tests/
โโโ auth/ # Authentication tests
โโโ caching/ # Cache performance tests
โโโ integration/ # Full-stack integration tests
โโโ iot/ # IoT device integration tests
โโโ queues/ # Queue system tests
โโโ results/ # Test results and reports
โโโ scripts/ # Test runner scripts
โโโ run-all-tests.js # Main integration test runner
โโโ README.md # Detailed documentation
Framework: Mixed (Node.js, Python, SQL) Coverage: Comprehensive cross-system testing Features: Performance testing, real-time subscriptions, IoT integration
๐ Running Tests
Local Development
Run All Tests
# Run everything (recommended for pre-commit)
./test-all.sh
# Or run specific test suites
./test-all.sh backend
./test-all.sh frontend
./test-all.sh integration
Backend Tests Only
cd backend
python -m pytest app/tests/ -v
Frontend Tests Only
cd frontend
npm test
Integration Tests Only
cd tests
node run-all-tests.js
GitHub Actions CI/CD
Tests run automatically on:
- Pull Requests to main
or develop
- Pushes to main
or develop
Workflow Structure
- Backend Tests (parallel)
- Python setup and caching
- Dependency installation
- Code formatting (black, isort)
- Type checking (mypy)
- Unit tests (pytest)
-
Coverage reporting
-
Frontend Tests (parallel)
- Node.js setup and caching
- Dependency installation
- Linting (ESLint)
- Type checking (TypeScript)
- Unit tests (Jest)
-
Coverage reporting
-
Integration Tests (after unit tests pass)
- Start backend service
- Run cross-cutting tests
- Performance tests (main branch only)
-
Test result collection
-
Test Summary
- Aggregate results
- Generate reports
- Upload artifacts
๐ Coverage & Quality
Current Coverage
- Backend: 80%+ requirement (enforced by pytest)
- Frontend: Not yet configured
- Integration: Comprehensive but not measured by coverage
Quality Gates
- Code Quality: black/isort (Python), ESLint (JavaScript/TypeScript)
- Type Checking: mypy (Python), TypeScript
- Security: Dependency scanning (planned)
- Performance: Baseline testing on main branch
๐ง Migration Recommendations
Immediate Actions (High Priority)
-
Expand Frontend Testing ๐
# Add Jest configuration cd frontend npm install --save-dev jest @testing-library/react @testing-library/jest-dom
-
Organize Backend Tests ๐
cd backend/app/tests mkdir -p unit integration # Move specific test files to appropriate directories
-
Clean Up Test Artifacts ๐งน
# Remove old test results (keeping directory structure) rm -rf frontend/test-results/.last-run.json
Medium-Term Improvements
- Add Component Tests โ๏ธ
- Test all React components
- Test custom hooks
-
Test utility functions
-
Improve Integration Coverage ๐
- Add API client tests
- Test user workflows
-
Add error scenario testing
-
Performance Monitoring ๐
- Set up continuous performance testing
- Add performance regression detection
- Monitor API response times
Long-Term Enhancements
- Visual Regression Testing ๐๏ธ
- Add Playwright or Cypress for E2E
- Screenshot comparison testing
-
Cross-browser testing
-
Contract Testing ๐ค
- API contract testing between frontend/backend
- Schema validation testing
-
Backward compatibility testing
-
Security Testing ๐
- Automated security scanning
- Penetration testing integration
- Dependency vulnerability scanning
๐ ๏ธ Development Workflow
Pre-Commit Checklist
# 1. Run all tests locally
./test-all.sh
# 2. Check coverage
cd backend && python -m pytest --cov=app --cov-report=html
# 3. Format and type check
cd backend && black . && isort . && mypy app/
cd frontend && npm run lint && npm run format
Adding New Tests
Backend Tests
# app/tests/test_new_feature.py
import pytest
from httpx import AsyncClient
@pytest.mark.asyncio
async def test_new_feature(setup_test_environment, client):
response = await client.get("/api/v1/new-feature")
assert response.status_code == 200
Frontend Tests
// frontend/tests/unit/components/NewComponent.test.js
import { render, screen } from '@testing-library/react';
import NewComponent from '@/components/NewComponent';
test('renders new component', () => {
render(<NewComponent />);
expect(screen.getByText('Expected Text')).toBeInTheDocument();
});
Integration Tests
// tests/integration/test_new_integration.js
// Add to existing test suite in tests/run-all-tests.js
๐ Troubleshooting
Common Issues
-
Backend Tests Failing
# Check dependencies cd backend && pip install -r requirements.txt # Check environment variables export TESTING=true export SUPABASE_URL=your_test_url
-
Frontend Tests Not Running
# Install dependencies cd frontend && npm install # Check test script exists grep '"test"' package.json
-
Integration Tests Timeout
# Ensure backend is running curl http://localhost:8000/health # Check Supabase connection node -e "console.log(process.env.SUPABASE_URL)"
Getting Help
- Backend Issues: Check
backend/app/tests/conftest.py
for fixture setup - Frontend Issues: Review Jest configuration and React Testing Library docs
- Integration Issues: See
tests/README.md
for comprehensive documentation - CI/CD Issues: Check
.github/workflows/tests.yml
and GitHub Actions logs
๐ฏ Success Metrics
Short-term Goals (1-2 weeks)
- [ ] Frontend test coverage > 50%
- [ ] All critical user paths tested
- [ ] CI/CD pipeline running smoothly
Medium-term Goals (1-2 months)
- [ ] Frontend test coverage > 80%
- [ ] Performance baseline established
- [ ] Security testing integrated
Long-term Goals (3-6 months)
- [ ] Visual regression testing
- [ ] Cross-browser E2E testing
- [ ] Automated performance monitoring
- [ ] Contract testing between services
Last Updated: January 2025 Maintained By: Development Team Next Review: February 2025