Backend Testing
The backend uses Django’s built-in test framework with unittest-xml-reporting for test reports.Running Backend Tests
Running Specific Tests
Run tests for a specific app:Test Coverage
Generate a test coverage report:htmlcov/index.html.
Writing Backend Tests
Django tests are located intests.py files within each app:
backend/clubs/tests.py
Test Database
Django automatically creates a test database for running tests:- Migrations are applied automatically
- Database is destroyed after tests complete
- Each test runs in a transaction that’s rolled back
Test Fixtures
Thepopulate management command can be used to create test data:
- Test users (username:
bfranklin, password:test) - Sample clubs
- Sample events
- Sample memberships
Frontend Testing
The frontend includes multiple types of tests: type checking, linting, and integration tests.Type Checking
Penn Clubs uses TypeScript for type safety. Run the type checker:Linting
Linting checks code style and catches common errors:- ESLint rules
- TypeScript-specific rules
- React best practices
- Import ordering
- Unused imports
Integration Tests
Cypress integration tests verify end-to-end functionality:test.sh script which:
- Starts the backend and frontend servers
- Waits for services to be ready
- Runs Cypress tests
- Generates coverage reports
Running Cypress Interactively
For debugging tests, run Cypress in interactive mode:- Run individual test files
- See tests execute in a browser
- Debug failures with browser DevTools
- Take screenshots and videos
Writing Frontend Tests
Cypress tests are located in thecypress/ directory:
cypress/integration/clubs.spec.js
Code Quality Tools
Backend: Ruff
Penn Clubs uses Ruff for Python linting and formatting:Ruff Configuration
Ruff is configured inbackend/pyproject.toml:
backend/pyproject.toml
Frontend: ESLint & Prettier
ESLint and Prettier ensure consistent code style:Pre-commit Hooks
Pre-commit hooks automatically run checks before each commit:- Code is properly formatted
- Linting passes
- No trailing whitespace
- Files end with newlines
Continuous Integration
GitHub Actions runs automated tests on every pull request.CI Workflow
-
Backend Tests
- Set up Python 3.13
- Install dependencies with uv
- Run Django test suite
- Upload coverage to Codecov
-
Frontend Tests
- Set up Node 20
- Install dependencies with Bun
- Run TypeScript type checking
- Run ESLint
- Run Cypress integration tests
- Upload coverage to Codecov
-
Docker Build
- Build backend Docker image
- Build frontend Docker image
- Run integration tests in containers
Docker Compose Testing
Thedocker-compose.test.yaml file defines a complete testing environment:
- PostgreSQL database
- Backend service
- Frontend service
- Traefik reverse proxy
- Backend migrations
- Integration tests
- Frontend build verification
Test Coverage Goals
Backend Coverage
- Minimum: 70% overall coverage
- Target: 80%+ overall coverage
- Critical paths: 90%+ coverage for authentication, payments, permissions
Frontend Coverage
- Type safety: 100% TypeScript (no
anywithout justification) - Integration tests: Critical user journeys covered
- Component tests: Reusable components tested in Storybook
Common Testing Issues
Backend: Database Errors
If you encounter database errors during tests:Frontend: Port Conflicts
If Cypress can’t connect to the app:- Ensure both backend and frontend are running
- Check that ports 8000 and 3000 are available
- Verify the proxy configuration in
frontend/package.json
Integration Test Timeouts
If integration tests timeout:Best Practices
Backend Testing
- Use
setUp()andtearDown()for test fixtures - Test both success and error cases
- Use factories for creating test data
- Mock external API calls
- Test permissions and authentication
- Test edge cases and boundary conditions
Frontend Testing
- Use data-testid attributes for reliable selectors
- Test user interactions, not implementation details
- Mock API responses for consistent tests
- Test accessibility with Cypress axe
- Test responsive layouts
- Keep tests independent and isolated
General
- Write tests before fixing bugs (TDD for bug fixes)
- Keep tests fast and focused
- Use descriptive test names
- Group related tests in classes/describes
- Clean up test data after tests
- Run tests locally before pushing
Running All Tests
To run the complete test suite:Next Steps
- Learn about System Architecture
- Set up your Local Development Environment
- Read the Deployment Guide