Skip to content

How to Run Tests

Quick start

cd backend

# Run all tests
make test

# Run with verbose output
make test-verbose

# Run a specific package
go test ./internal/controller/... -v

# Run a single test function
go test ./internal/controller -run TestLogin -v

Available Make targets

make test              # Run all tests
make test-verbose      # Run tests with detailed output
make test-coverage     # Generate coverage report
make coverage-html     # Generate HTML coverage report
make test-unit         # Run only unit tests
make test-auth         # Run auth/authz tests specifically
make test-short        # Skip long-running tests
make test-race         # Run with race detector
make benchmark         # Run benchmark tests
make benchmark-auth    # Run auth-specific benchmarks
make clean-testcache   # Clear Go test cache
make help              # Show all available targets

Running individual packages

# Password utilities
go test ./internal/util -run Hash -v

# JWT token utilities
go test ./internal/util -run Token -v

# Login controller
go test ./internal/controller -run Login -v

# Parent messages feature
go test ./internal/controller -run ParentMessages -v

# User role methods
go test ./internal/model -run User -v

# Authorization middleware
go test ./internal/middleware -v

# Integration tests
go test ./internal/integrationtesting -v

Coverage reports

# Terminal coverage summary
make test-coverage

# HTML coverage report (opens in browser)
make coverage-html
# Then open coverage.html in your browser

# Show function-level coverage
go tool cover -func=coverage.out

# Coverage for a specific package
go test ./internal/util -coverprofile=util_coverage.out
go tool cover -html=util_coverage.out

Benchmarks

# Run all benchmarks
make benchmark

# Run specific benchmark
go test ./internal/util -bench=BenchmarkGenerateHashPassword -benchmem

# Compare benchmark results
go test ./internal/util -bench=. -benchmem > old.txt
# ... make changes ...
go test ./internal/util -bench=. -benchmem > new.txt
benchcmp old.txt new.txt

Troubleshooting

"No children displayed" for parent accounts

Make sure the server is running in debug mode. Test data is only initialized when the server starts with APP_TESTING=true or in debug mode.

  1. Stop the server if running
  2. Start in debug mode: go run cmd/app-server/main.go
  3. Check console output for "=== Test Data Initialization Complete ==="
  4. Log in with a test account

Database connection errors

Tests use in-memory SQLite. Ensure the sqlite3 driver is available:

go mod download
go mod tidy

JWT secret missing

Error: could not decode secret

Set APP_SECRET in the test:

secret := testhelpers.GetTestSecret()
os.Setenv("APP_SECRET", secret)
defer os.Unsetenv("APP_SECRET")

Test cache issues

Tests not reflecting code changes:

make clean-testcache
# or
go clean -testcache

Race condition detection

make test-race

Debugging a single test

# Verbose output for one test
go test ./internal/util -run TestGenerateHashPassword -v

# Show coverage for a single test
go test ./internal/util -run TestGenerateHashPassword -cover