Testing
Test against a real database. ant has no in-memory fake: a fake accepts SQL the real server would refuse and hides what constraints, triggers and transactions do. PostgreSQL in a container starts in seconds.
Set up a database for tests
Build the schema from your real migrations, so a broken migration fails a test before it reaches production.
var shared *models.AppDB
func TestMain(m *testing.M) {
ctx := context.Background()
db, err := ant.Open[models.AppDB](ctx, postgres.Dialect, os.Getenv("TEST_DATABASE_URL"))
if err != nil {
log.Fatal(err)
}
if err := migrate.Up(ctx, db, migrate.Config{FS: migrations.Files}); err != nil {
log.Fatal(err)
}
shared = db
os.Exit(m.Run())
}
For quick experiments, db.EnsureCreated(ctx) builds tables straight from the structs.
# docker-compose.yml
services:
db:
image: postgres:17
environment: { POSTGRES_USER: app, POSTGRES_PASSWORD: app, POSTGRES_DB: app }
ports: ["127.0.0.1:55432:5432"]
Skip when no database is set
A test that needs the database skips itself when TEST_DATABASE_URL is empty, so go test ./... works anywhere:
func TestNeedsDatabase(t *testing.T) {
if os.Getenv("TEST_DATABASE_URL") == "" {
t.Skip("TEST_DATABASE_URL is not set")
}
}
A skipped test is not a passing test. Check the skip count in CI.
Roll each test back
anttest.Isolate gives a test a context bound to a transaction that rolls back when the test ends, so tests share a database without cleanup.
func TestSignup(t *testing.T) {
db := anttest.Isolate(t, shared)
err := db.Users.Insert(t.Context(), &models.User{Name: "Ann", Email: "ann@example.com"})
if err != nil {
t.Fatal(err)
}
}
Code that commits for real, such as through a second connection, doesn't see the test's writes, so a concurrency test needs a database of its own. An isolated context is one connection, so don't share it between goroutines.
Make entities with factories
A factory makes valid entities, so a test spells out only what it needs. Its number argument keeps values unique.
var UserFactory = anttest.NewFactory(func(n int) *models.User {
return &models.User{
Name: fmt.Sprintf("user %d", n),
Email: fmt.Sprintf("u%d@example.com", n),
}
})
func TestFactories(t *testing.T) {
db := anttest.Isolate(t, shared)
user := UserFactory.Create(t, &db.Users) // in the database
draft := UserFactory.Build() // in memory
many := UserFactory.CreateMany(t, &db.Users, 10) // several
young := UserFactory.Create(t, &db.Users, func(u *models.User) { u.Age = 5 }) // one override
}
Name a state
With returns a new factory with the changes applied.
func TestAdmin(t *testing.T) {
db := anttest.Isolate(t, shared)
admin := UserFactory.With(func(u *models.User) { u.Admin = true })
boss := admin.Create(t, &db.Users)
}
AfterBuild and AfterCreate add callbacks that run on each entity.
Share one parent
anttest.Recycle creates one parent the first time a child needs it and hands the same one to the rest.
func TestRecycle(t *testing.T) {
db := anttest.Isolate(t, shared)
author := anttest.Recycle(UserFactory, &db.Users)
posts := anttest.NewFactory(func(n int) *models.Post {
return &models.Post{Title: fmt.Sprintf("post %d", n), UserID: author.Get(t).ID}
})
posts.CreateMany(t, &db.Posts, 3) // one author
}
Load fixtures
anttest.Load loads rows from JSON files named after their tables, for data that doesn't change:
[{"id": 1, "name": "Ada", "created_at": "2026-01-01T00:00:00Z"}]
Rows go in as written, with no hooks, Go defaults or timestamps, so parents come first. Names are quoted for the context's database. Values are passed as the file has them, and MySQL and MariaDB don't read 2026-01-01T00:00:00Z, so fixtures for them write 2026-01-01 00:00:00.
//go:embed testdata
var data embed.FS
func TestWithFixtures(t *testing.T) {
db := anttest.Isolate(t, shared)
anttest.Load(t, db, data, "testdata/users.json", "testdata/posts.json")
}
Check the SQL
q.ToSQL() returns a query's SQL without a database, which suits testing a query builder. Otherwise run the query.
ant.DryRun runs real code in a transaction and returns every statement, which shows what a unit of work decides to write. antvet catches mistakes before tests run.
SQLite has no FOR UPDATE, no enum types, different date handling and looser typing, so a test can pass there and fail in production. Test on the database you deploy. Where that is SQLite, ant's dialect runs queries and writes, but migrations aren't available, so create the tables with your own SQL.
See it tested: tests/features/testing