Skip to content

๐Ÿงช 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

  1. Backend Tests (parallel)
  2. Python setup and caching
  3. Dependency installation
  4. Code formatting (black, isort)
  5. Type checking (mypy)
  6. Unit tests (pytest)
  7. Coverage reporting

  8. Frontend Tests (parallel)

  9. Node.js setup and caching
  10. Dependency installation
  11. Linting (ESLint)
  12. Type checking (TypeScript)
  13. Unit tests (Jest)
  14. Coverage reporting

  15. Integration Tests (after unit tests pass)

  16. Start backend service
  17. Run cross-cutting tests
  18. Performance tests (main branch only)
  19. Test result collection

  20. Test Summary

  21. Aggregate results
  22. Generate reports
  23. 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)

  1. Expand Frontend Testing ๐Ÿ“ˆ

    # Add Jest configuration
    cd frontend
    npm install --save-dev jest @testing-library/react @testing-library/jest-dom
    

  2. Organize Backend Tests ๐Ÿ“

    cd backend/app/tests
    mkdir -p unit integration
    # Move specific test files to appropriate directories
    

  3. Clean Up Test Artifacts ๐Ÿงน

    # Remove old test results (keeping directory structure)
    rm -rf frontend/test-results/.last-run.json
    

Medium-Term Improvements

  1. Add Component Tests โš›๏ธ
  2. Test all React components
  3. Test custom hooks
  4. Test utility functions

  5. Improve Integration Coverage ๐Ÿ”—

  6. Add API client tests
  7. Test user workflows
  8. Add error scenario testing

  9. Performance Monitoring ๐Ÿ“Š

  10. Set up continuous performance testing
  11. Add performance regression detection
  12. Monitor API response times

Long-Term Enhancements

  1. Visual Regression Testing ๐Ÿ‘๏ธ
  2. Add Playwright or Cypress for E2E
  3. Screenshot comparison testing
  4. Cross-browser testing

  5. Contract Testing ๐Ÿค

  6. API contract testing between frontend/backend
  7. Schema validation testing
  8. Backward compatibility testing

  9. Security Testing ๐Ÿ”’

  10. Automated security scanning
  11. Penetration testing integration
  12. 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

  1. Backend Tests Failing

    # Check dependencies
    cd backend && pip install -r requirements.txt
    
    # Check environment variables
    export TESTING=true
    export SUPABASE_URL=your_test_url
    

  2. Frontend Tests Not Running

    # Install dependencies
    cd frontend && npm install
    
    # Check test script exists
    grep '"test"' package.json
    

  3. 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