Integration Tests
Use integration tests when behavior depends on database semantics, generated accessors, or delivery through a real backend. The photo service is a small starting point: save through Service, close the connection, then read the record through a new service and repository.
Command
Run tagged integration tests from the Project root:
go test -tags=integration ./...The tag includes files guarded by //go:build integration; ordinary tests still run. Choose this convention for checks your team wants to run separately. Deterministic local database tests can also remain in the normal suite.
What Belongs Here
After Save and List Photos, create internal/photos/service_integration_test.go. Replace your/module in the imports with your module path. Each run uses its own temporary SQLite file.
//go:build integration
package photos
import (
"context"
"path/filepath"
"testing"
"your/module/internal/database"
"your/module/internal/inspects"
)
// TestPhotoServicePersistsAcrossConnections verifies saved metadata survives reopening SQLite.
func TestPhotoServicePersistsAcrossConnections(t *testing.T) {
t.Setenv("DB_DEFAULT", "default")
t.Setenv("DB_DRIVER", "sqlite")
t.Setenv("DB_DATABASE", filepath.Join(t.TempDir(), "photos.db"))
connections := database.NewConnections(inspects.NewManager())
t.Cleanup(func() { _ = connections.Close(context.Background()) })
db, err := connections.Default()
if err != nil {
t.Fatal(err)
}
if err := db.AutoMigrate(&Photo{}); err != nil {
t.Fatal(err)
}
service := NewService(NewPhotoRepo(connections))
saved, err := service.Save(context.Background(), "sunrise.jpg")
if err != nil {
t.Fatal(err)
}
if saved.ID <= 0 || saved.StorageKey != "sunrise.jpg" {
t.Fatalf("saved = %+v, want assigned ID and sunrise.jpg", saved)
}
if err := connections.Close(context.Background()); err != nil {
t.Fatal(err)
}
reopened := database.NewConnections(inspects.NewManager())
t.Cleanup(func() { _ = reopened.Close(context.Background()) })
photos, err := NewService(NewPhotoRepo(reopened)).ListNewest(context.Background())
if err != nil {
t.Fatal(err)
}
if len(photos) != 1 || photos[0] != saved {
t.Fatalf("photos = %+v, want [%+v]", photos, saved)
}
}Run this case directly:
go test -tags=integration ./internal/photos -run TestPhotoServicePersistsAcrossConnections -count=1Expected result: the package reports ok; a fresh connection reads the same ID and storage key. The test exercises the actual service, generated repository, and database accessor without starting HTTP. The repository query test separately checks ordering with two records.
AutoMigrate creates only this test's fixture table. Verify the application's SQL migrations separately against a disposable database. Use the deployment database driver when testing SQL behavior that can differ from SQLite. Add runtime checks when generated registration, readiness, middleware, or worker delivery is the behavior under test.
Testcontainers
Use container-backed tests when a real database, Redis, NATS, or another broker is required to prove the behavior. State the Docker, emulator, or credential prerequisites beside the test command and run a focused package while developing it.
Give each test isolated records, queue names, or storage paths and clean them up. Verify the property the local test cannot establish, such as transaction isolation, broker redelivery, or cross-process visibility. Rendered App Smoke Tests owns framework template and generator validation.
Environment
Apps include a committed .env.testing as their safe test profile. Code that loads goforj/env selects it when APP_ENV=testing or Go test markers are present. Process environment variables take precedence; the SQLite test uses t.Setenv to point its database at t.TempDir().
Run the configuration contract check before tests in CI:
forj env:check
go test ./...Expected result: the environment contract check and package tests pass. Use go test -tags=integration ./... as the additional integration job when adopting the tagged convention above.
Keep the generated deterministic defaults for ordinary tests. Database, Redis, and SMTP host keys use loopback defaults; generated NATS and RabbitMQ URLs use their local container service names. Supply only the additional process variables needed by tests that intentionally contact a live backend. When a separate harness starts a binary, pass APP_ENV=testing and isolated backend settings in its child environment so the binary selects the test profile too.
On upgrade, review and remove an unmanaged legacy .env.testing before generation creates the committed profile. Keep tests independent of a developer's local .env unless loading that file is the behavior under test.
Next Steps
- HTTP Tests verifies registered routes and middleware.
- Job and Queue Tests verifies photo payloads and local dispatch.
- Rendered App Smoke Tests covers framework-maintainer checks.