Skip to main content

Testing Framework: Test Database Schemas, Migrations, and Plans

The Atlas testing framework runs unit and integration tests against your database. Schema tests verify functions, views, triggers, and constraints, and migration and plan tests check what each change does to the data. Test files hold the structure in HCL and the logic in plain SQL, and every case runs on a clean dev database, locally and in CI.

Tests can load production-like rows from a dataset and profile every statement they run: the locks it takes, the tables it rewrites, and the space it needs. Profile expectations set bounds on those numbers, so a change that would block production fails the test before it is merged. Data Scripts are tested the same way. Datasets and profiling are in preview.

Test every change before it merges

Whether a person or a coding agent wrote the change, the same tests run in the agent's session and again in CI. Profile expectations fail a change that would block a production table.

$ atlas migrate test --dev-url "docker://postgres/18/dev" .-- FAIL: index (1.73s)    migrate.test.hcl:43:        Error: db.lock.time: public.orders (ShareLock, blocks        writes): ~19.3s at 50,000,000 rows, expected less than        or equal to 5s (3_status_index.sql:1)-- FAIL: retype (2.61s)    migrate.test.hcl:61:        Error: db.table.rewrites: public.orders: 1, expected        equal to 0 (6_retype.sql:1)               db.lock.time: public.orders (AccessExclusiveLock,               blocks reads): ~48.6s at 50,000,000 rows,               expected less than or equal to 5s               (6_retype.sql:1)               db.table.copy: public.orders: ~5.7GB at               50,000,000 rows, expected less than or equal to               1GB (6_retype.sql:1)

Test Types​

Atlas has a test command for each workflow: atlas schema test for the schema, atlas migrate test for versioned migrations, atlas schema plan test for declarative migration plans, and atlas script test for Data Scripts. A test case is a block named after its kind: test "schema", test "migrate", test "plan", or test "script". The examples below cover the four kinds, plus three capabilities they build on: table-driven cases, datasets, and profiling.

Get startedSign up

Testing requires Atlas Pro. New to Atlas Pro? Sign up for a free 30-day trial, or log in from the terminal:

atlas login

A schema test runs against the desired schema. Atlas creates the schema on the dev database before each test case and cleans it up after. exec expects a statement to pass, and catch expects one to fail:

schema.test.hcl
test "schema" "postal" {
parallel = true
exec {
sql = "select '12345'::us_postal_code"
}
catch {
sql = "select 'hello'::us_postal_code"
}
}
atlas schema test --dev-url "docker://postgres/15/dev" --url "file://schema.hcl" .

On PostgreSQL, schema tests can also call pgTAP assertion functions. See pgTAP and Atlas.

Running Tests​

Every test command runs on a dev database, passed with --dev-url. Pass a directory to run the test files in it, or name a test file. Each command reads what it tests from its own source:

  • atlas schema test runs test "schema" cases against --url: a database URL, an HCL, SQL, or ORM schema, or a migration directory.
  • atlas migrate test runs test "migrate" cases against --dir, the migration directory (file://migrations by default).
  • atlas schema plan test runs test "plan" cases, each applying the plan file in its apply block.
  • atlas script test runs test "script" cases, and each script command names the file it loads.

Add --run with a regular expression to run only the matching tests, or --env to read the URLs from an env block in atlas.hcl:

atlas schema test --env local schema.test.hcl

Get Started with a Coding Agent​

With a coding agent, install the Atlas agent skills. One command installs them:

Install the skills from a terminal. It installs the Atlas plugin, with all its skills, for every project on this machine.

claude plugin marketplace add ariga/atlas
claude plugin install atlas@ariga

Then start the onboarding skill in Claude Code. See Claude Code with Atlas for the other ways to use the skills.

/atlas:atlas-onboard

The atlas skill runs atlas schema test and atlas migrate test on the changes the agent makes, before it reports them done. To set up Atlas in a new project, start the atlas-onboard skill.

Next Steps​