Skip to main content

Testing Infrastructure Summary

What Was Implemented

This document summarizes the complete testing infrastructure rebuild for the Gofannon project.

Key Achievements

Full test coverage infrastructure for frontend and backend ✅ 95% minimum coverage requirement enforced via CI/CD ✅ Automated PR checks run unit tests on every pull request ✅ Nightly integration tests run comprehensive tests daily ✅ Comprehensive documentation for writing and maintaining tests ✅ Example tests demonstrating best practices ✅ Test data factories for generating consistent test fixtures

Testing Strategy

Test Types

TypeLocationRun WhenPurpose
Unit Tests (Frontend)packages/webui/src/**/*.test.jsxEvery PRTest React components in isolation
Unit Tests (Backend)packages/api/user-service/tests/unit/Every PRTest Python functions/classes in isolation
Integration Testspackages/api/user-service/tests/integration/NightlyTest with real services (DB, S3, etc.)
E2E Testswebapp/tests/e2e/NightlyTest complete user workflows

Coverage Requirements

  • Minimum: 95% for lines, functions, branches, statements
  • Enforced: CI/CD blocks PRs below threshold
  • Reported: Codecov integration for tracking over time

Infrastructure Components

Frontend Testing (Vitest)

Files Created:

Dependencies Added:

  • vitest - Test runner
  • @testing-library/react - React component testing utilities
  • @testing-library/user-event - User interaction simulation
  • @testing-library/jest-dom - DOM matchers
  • @vitest/ui - Interactive test UI
  • @vitest/coverage-v8 - Coverage reporting
  • jsdom - DOM environment for Node

Commands:

pnpm test                  # Run tests
pnpm test:ui # Interactive UI
pnpm test:coverage # With coverage report

Backend Testing (pytest)

Files Created:

Dependencies Added:

  • pytest>=8.0.0 - Test framework
  • pytest-asyncio>=0.23.0 - Async test support
  • pytest-cov>=4.1.0 - Coverage plugin
  • pytest-mock>=3.12.0 - Mocking utilities
  • factory-boy>=3.3.0 - Test data factories
  • faker>=22.0.0 - Fake data generation

Commands:

python -m pytest tests/unit              # Unit tests
python -m pytest tests/integration # Integration tests
python -m pytest --cov=. --cov-report=html # With coverage

CI/CD Workflows

Files Created:

  1. pr-unit-tests.yml - Runs on every PR

    • Frontend unit tests with coverage
    • Backend unit tests with coverage
    • Lint checks
    • Coverage threshold validation
    • Codecov upload
  2. nightly-integration-tests.yml - Runs daily at 2 AM UTC

    • Backend integration tests with full Docker stack
    • E2E tests with Playwright
    • Uses GitHub Secrets for API keys (OPENAI_API_KEY, AWS credentials)
    • Collects logs on failure
    • Sends notifications on failure

Secrets Required:

  • OPENAI_API_KEY - For AI model testing
  • GEMINI_API_KEY - For alternative AI model
  • AWS_ACCESS_KEY_ID - For S3 integration tests
  • AWS_SECRET_ACCESS_KEY - For S3 integration tests
  • AWS_REGION - AWS region (optional, defaults to us-east-1)

NPM Scripts

Updated package.json:

{
"test": "pnpm run test:unit && pnpm run test:integration",
"test:unit": "pnpm run test:unit:frontend && pnpm run test:unit:backend",
"test:unit:frontend": "pnpm --filter webui test",
"test:unit:backend": "cd packages/api/user-service && python -m pytest tests/unit -v",
"test:integration": "pnpm run test:integration:backend && pnpm run test:e2e",
"test:integration:backend": "cd packages/api/user-service && python -m pytest tests/integration -v",
"test:e2e": "playwright test",
"test:coverage": "pnpm run test:coverage:frontend && pnpm run test:coverage:backend",
"test:coverage:frontend": "pnpm --filter webui test:coverage",
"test:coverage:backend": "cd packages/api/user-service && python -m pytest tests/unit --cov=. --cov-report=html --cov-report=term-missing"
}

Documentation

Created in docs/testing/:

  1. README.md - Testing overview and quick start
  2. unit-testing.md - Comprehensive unit testing guide
  3. contributing.md - PR requirements and guidelines
  4. SUMMARY.md - This document

Topics Covered:

  • Testing philosophy and strategy
  • Running tests locally
  • Writing unit tests (frontend & backend)
  • Using test factories
  • Mocking dependencies
  • Testing async code
  • Coverage requirements
  • PR requirements
  • Best practices
  • Common patterns
  • Troubleshooting

Test Examples

Backend Unit Test Example

class TestUserService:
@pytest.fixture
def mock_db(self):
db = Mock()
db.get = Mock(return_value={"id": "test-123"})
return db

def test_get_user_existing(self, user_service, mock_db):
user = user_service.get_user("test-123")
mock_db.get.assert_called_once_with("users", "test-123")
assert user.id == "test-123"

Frontend Unit Test Example

describe('ActionCard', () => {
it('calls onClick when clicked', async () => {
const onClick = vi.fn();
render(<ActionCard {...props} onClick={onClick} />);

await userEvent.click(screen.getByRole('button'));

expect(onClick).toHaveBeenCalled();
});
});

Project Structure

webapp/
├── packages/
│ ├── webui/ # Frontend
│ │ ├── src/
│ │ │ ├── components/*.test.jsx # Component tests
│ │ │ └── test/
│ │ │ ├── setup.ts # Test setup
│ │ │ └── *.test.js # Utility tests
│ │ └── vitest.config.ts # Vitest config
│ │
│ └── api/user-service/ # Backend
│ ├── tests/
│ │ ├── conftest.py # Pytest fixtures
│ │ ├── unit/ # Unit tests
│ │ ├── integration/ # Integration tests
│ │ └── factories/ # Test data factories
│ ├── pytest.ini # Pytest config
│ └── .coveragerc # Coverage config

└── package.json # NPM scripts

.github/workflows/
├── pr-unit-tests.yml # PR checks
└── nightly-integration-tests.yml # Nightly tests

docs/testing/
├── README.md # Overview
├── unit-testing.md # Unit test guide
├── contributing.md # PR requirements
└── SUMMARY.md # This file

Usage Guide

For Developers

When writing new code:

  1. Write tests first (TDD)
  2. Ensure 95% coverage on new files
  3. Run pnpm test:unit before committing
  4. PR checks will run automatically

When reviewing PRs:

  1. Verify tests exist for new code
  2. Check coverage reports
  3. Run tests locally if needed
  4. Ensure CI checks pass

For Contributors

Required for every PR:

  • Unit tests for new/modified code
  • 95% minimum coverage
  • All tests passing
  • No lint errors

See contributing.md for detailed guidelines.

Maintenance

Adding New Tests

Backend:

# Create test file
touch tests/unit/test_new_feature.py

# Write tests following examples
# Run to verify
python -m pytest tests/unit/test_new_feature.py -v

Frontend:

# Create test file next to component
touch src/components/NewComponent.test.jsx

# Write tests following examples
# Run to verify
pnpm test NewComponent.test.jsx

Updating Coverage Thresholds

Frontend: Edit vitest.config.ts

coverage: {
thresholds: {
lines: 95, // Adjust here
functions: 95,
branches: 95,
statements: 95,
}
}

Backend: Edit pytest.ini

addopts =
--cov-fail-under=95 # Adjust here

Adding GitHub Secrets

  1. Go to repository Settings → Secrets and variables → Actions
  2. Click "New repository secret"
  3. Add required secrets:
    • OPENAI_API_KEY
    • AWS_ACCESS_KEY_ID
    • AWS_SECRET_ACCESS_KEY
    • AWS_REGION (optional)

Benefits

For Development

  • ✅ Catch bugs early
  • ✅ Faster debugging
  • ✅ Safe refactoring
  • ✅ Living documentation
  • ✅ Better code design

For Collaboration

  • ✅ Consistent quality bar
  • ✅ Easier code reviews
  • ✅ Onboarding documentation
  • ✅ Prevents regressions
  • ✅ CI/CD confidence

For Maintenance

  • ✅ Easy to add features
  • ✅ Safe to modify code
  • ✅ Coverage tracking
  • ✅ Automated validation
  • ✅ Long-term stability

Next Steps

  1. Install dependencies:

    cd webapp
    pnpm install
    cd packages/api/user-service
    pip install -r requirements.txt
  2. Run tests:

    cd webapp
    pnpm test:unit
  3. Check coverage:

    pnpm test:coverage
  4. Read the guides:

  5. Write your first test:

    • Pick a file without tests
    • Follow the examples
    • Aim for 100% coverage
    • Run and verify

Support

  • Documentation: Check docs/testing/
  • Examples: Review existing test files
  • Questions: Ask in team chat or PR comments
  • Issues: Open a GitHub issue for testing infrastructure problems

Testing infrastructure implemented: January 2025 Documentation version: 1.0