-
Notifications
You must be signed in to change notification settings - Fork 0
TESTING
This document outlines the comprehensive testing strategy for NDF Studio, including unit tests, integration tests, end-to-end tests, and automated CI/CD pipeline.
/\
/ \ E2E Tests (Few, Critical Paths)
/____\
/ \ Integration Tests (API, Components)
/________\ Unit Tests (Many, Fast)
- Unit Tests - Fast, isolated component/function tests
- Integration Tests - API endpoint and component interaction tests
- End-to-End Tests - Full user workflow tests
- Security Tests - Vulnerability scanning and security checks
- Performance Tests - Load and stress testing (future)
# Frontend testing dependencies
cd frontend
npm install
# Backend testing dependencies
cd ../backend
pip install pytest pytest-cov pytest-asyncio httpx
# Frontend tests
cd frontend
npm run test:all
# Backend tests
cd ../backend
python -m pytest tests/ -v --cov=core
Location: frontend/src/test/
Run Commands:
npm run test # Run tests once
npm run test:watch # Run tests in watch mode
npm run test:ui # Run tests with UI
npm run test:coverage # Run tests with coverage report
Example Test Structure:
import { render, screen, fireEvent } from '@testing-library/react'
import { describe, it, expect, vi } from 'vitest'
import MyComponent from '../MyComponent'
describe('MyComponent', () => {
it('should render correctly', () => {
render(<MyComponent />)
expect(screen.getByText('Hello')).toBeInTheDocument()
})
it('should handle user interactions', () => {
render(<MyComponent />)
fireEvent.click(screen.getByRole('button'))
expect(screen.getByText('Clicked!')).toBeInTheDocument()
})
})
Location: frontend/tests/e2e/
Run Commands:
npm run test:e2e # Run E2E tests
npm run test:e2e:ui # Run E2E tests with UI
npm run test:e2e:headed # Run E2E tests in headed mode
Example E2E Test:
import { test, expect } from '@playwright/test'
test('user can login and create a graph', async ({ page }) => {
await page.goto('/')
// Login
await page.fill('[data-testid="username"]', 'testuser')
await page.fill('[data-testid="password"]', 'password')
await page.click('[data-testid="login-button"]')
// Create graph
await page.click('[data-testid="new-graph"]')
await page.fill('[data-testid="graph-name"]', 'My Graph')
await page.click('[data-testid="create-button"]')
// Verify
await expect(page.getByText('My Graph')).toBeVisible()
})
- Unit Tests: 80%+ coverage
- Integration Tests: Critical API endpoints
- E2E Tests: Core user workflows
Location: backend/tests/
Run Commands:
python -m pytest tests/ -v # Run all tests
python -m pytest tests/ -v --cov=core # With coverage
python -m pytest tests/ -k "test_auth" # Run specific tests
Example Test Structure:
import pytest
from fastapi.testclient import TestClient
from backend.main import app
client = TestClient(app)
def test_create_user():
response = client.post("/auth/register", json={
"username": "testuser",
"email": "[email protected]",
"password": "password123"
})
assert response.status_code == 201
assert response.json()["username"] == "testuser"
def test_login_user():
response = client.post("/auth/login", data={
"username": "testuser",
"password": "password123"
})
assert response.status_code == 200
assert "access_token" in response.json()
Location: backend/tests/integration/
Tests for:
- Authentication flows
- Graph CRUD operations
- Node and relation management
- Schema validation
- Error handling
Backend (Bandit):
pip install bandit
bandit -r backend/ -f json -o bandit-report.json
Frontend (npm audit):
cd frontend
npm audit --audit-level=moderate
-
Authentication Testing
- Test token validation
- Test authorization headers
- Test session management
-
Input Validation
- SQL injection prevention
- XSS prevention
- CSRF protection
-
API Security
- Rate limiting
- CORS configuration
- HTTPS enforcement
Triggered on:
- Push to
main
ordevelop
branches - Pull requests to
main
ordevelop
Pipeline Stages:
- Backend Tests - Unit tests, integration tests, coverage
- Frontend Tests - Unit tests, linting, type checking
- E2E Tests - Full application testing
- Security Scan - Vulnerability scanning
- Build Check - Production build verification
Create .git/hooks/pre-commit
:
#!/bin/bash
set -e
echo "Running pre-commit checks..."
# Frontend checks
cd frontend
npm run lint
npm run test -- --run
npm run type-check
# Backend checks
cd ../backend
python -m pytest tests/ -v --tb=short
echo "All checks passed!"
Frontend: frontend/coverage/
- HTML coverage report
- LCOV format for CI
Backend: backend/htmlcov/
- HTML coverage report
- XML format for CI
Location: frontend/playwright-report/
- HTML test report
- Screenshots on failure
- Video recordings
- Trace files
-
Component Testing
- Test component rendering
- Test user interactions
- Test state changes
- Mock external dependencies
-
Accessibility Testing
- Use
@testing-library/jest-dom
matchers - Test keyboard navigation
- Test screen reader compatibility
- Use
-
Performance Testing
- Test component render times
- Test bundle size
- Test memory leaks
-
API Testing
- Test all HTTP methods
- Test error responses
- Test authentication/authorization
- Test input validation
-
Database Testing
- Use test database
- Clean up after tests
- Test transactions
- Test migrations
-
Integration Testing
- Test external service integration
- Test file system operations
- Test concurrent operations
Create .env.test
:
# Test database
DATABASE_URL=sqlite:///./test.db
# Test API settings
API_BASE_URL=http://localhost:8000
# Test user credentials
TEST_USERNAME=testuser
TEST_PASSWORD=testpass
Location: tests/fixtures/
- Sample graphs
- Test users
- Mock responses
# Install k6
npm install -g k6
# Run load test
k6 run tests/performance/load-test.js
# Run stress test
k6 run tests/performance/stress-test.js
# Debug unit tests
npm run test:ui
# Debug E2E tests
npm run test:e2e:headed
# Debug with pdb
python -m pytest tests/ -s --pdb
# Debug specific test
python -m pytest tests/test_auth.py::test_login -s --pdb
- Always for new features
- Always for bug fixes
- Always for critical paths
- Consider for refactoring
test_[functionality]_[scenario]_[expected_result]
Examples:
test_login_with_valid_credentials_should_succeed
test_create_graph_without_auth_should_fail
test_delete_node_should_remove_from_graph
tests/
βββ unit/ # Unit tests
βββ integration/ # Integration tests
βββ e2e/ # End-to-end tests
βββ fixtures/ # Test data
βββ utils/ # Test utilities
Before committing code:
- Unit tests pass
- Integration tests pass
- E2E tests pass
- Code coverage meets targets
- Linting passes
- Type checking passes
- Security scan passes
- Performance tests pass (if applicable)