This document describes the test suite for the IPE application.
The test suite is organized into several categories:
- Location: Co-located with source files (
*_test.go) - Packages:
api,app,channel,connection,events,storage,utils,websockets,concurrency - Requirements: No external dependencies
- Location:
integration/integration_test.go - Requirements: Redis server (optional - tests skip if unavailable)
- Purpose: End-to-end scenarios combining WebSocket and HTTP API
- Location:
redis/redis_test.go - Requirements: Redis server running
- Purpose: Test Redis client functionality for cross-instance scaling
- Location:
app/webhooks_test.go - Requirements: None (uses test HTTP server)
- Purpose: Test webhook delivery and payload validation
make test
# or
go test ./...make test-unit
# or
go test ./api/... ./app/... ./channel/... ...Requires Redis to be running:
# Start Redis (if not running)
docker run -d -p 6379:6379 redis:7-alpine
# Run integration tests
make test-integration
# or
REDIS_HOST=localhost REDIS_PORT=6379 go test ./redis/... ./integration/...make test-race
# or
go test -race ./...make test-coverage
# Opens coverage.html in browsergo test -v ./websockets/...
go test -v ./api/...The test suite aims for:
- Critical paths: 100% coverage (WebSocket, API handlers, auth)
- Overall: >85% code coverage
- All tests passing: Required before deployment
View coverage report:
make test-coverage
open coverage.htmlTests run automatically on:
- Push to main/master/develop branches
- Pull requests
- Scheduled runs (weekly)
-
CI Workflow (
.github/workflows/ci.yml)- Runs all tests with race detector
- Runs linters
- Builds for multiple platforms
- Security scanning
-
Test Workflow (
.github/workflows/test.yml)- Separate unit, integration, and webhook test jobs
- Coverage reporting
- Test matrix across Go versions
-
Lint Workflow (
.github/workflows/lint.yml)- Code formatting checks
- Static analysis
- golangci-lint
-
Build Workflow (
.github/workflows/build.yml)- Multi-platform builds
- Automatic releases on tags
-
CodeQL Workflow (
.github/workflows/codeql.yml)- Security analysis
- Vulnerability detection
make ci
# Runs: fmt-check, vet, lint, test-race# Install dependencies
make deps
# Install test tools
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
# For integration tests, start Redis
docker run -d -p 6379:6379 --name redis-test redis:7-alpine# Run a specific test
go test -v -run TestWebSocket_ConnectionEstablishment_ValidProtocol ./websockets/...
# Run tests matching a pattern
go test -v -run "Subscribe" ./app/...
# Run with verbose output
go test -v ./...The testutils package provides:
NewTestApp()- Create test applicationsNewTestStorage()- Create in-memory storageNewWebSocketTestServer()- Create test WebSocket serverConnectWebSocket()- Helper for WebSocket connectionsGetAppFromStorage()- Retrieve app from storage
The mocks package provides:
NewMockSocket()- Mock WebSocket for testing- Message tracking and verification
- Follow naming convention:
TestFunctionName_Scenario - Use table-driven tests for multiple scenarios
- Test error cases as well as success cases
- Use subtests for related test cases
- Clean up resources (connections, channels, etc.)
Example:
func TestFunctionName_Success(t *testing.T) {
// Arrange
app := testutils.NewTestApp()
// Act
result, err := app.DoSomething()
// Assert
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if result == nil {
t.Error("Expected result to be non-nil")
}
}- Ensure Redis is running:
redis-cli ping - Check environment variables:
REDIS_HOST,REDIS_PORT,REDIS_DB - Integration tests will skip if Redis is unavailable
- Run with
-raceflag:go test -race ./... - Fix data races before merging
- Run coverage:
make test-coverage - Review coverage.html to identify untested code
- Add tests for critical paths
- Isolation: Each test should be independent
- Deterministic: Tests should produce consistent results
- Fast: Unit tests should run quickly
- Clear: Test names should describe what they test
- Comprehensive: Cover both success and failure paths