Documentation
¶
Overview ¶
Package pgtest gives integration tests a real, migrated Postgres database.
Each call to New creates a fresh database on the server at TEST_DATABASE_URL and drops it when the test ends, so tests never share rows and can run in parallel. The database is cloned from a template migrated once per set of migrations, so a test pays for a copy, not for running every migration again:
func TestCreateUser(t *testing.T) {
pool := pgtest.New(t, migrations.FS)
users := repositories.NewUserRepository(sqlc.New(pool))
...
}
Without TEST_DATABASE_URL the test is skipped. Set REQUIRE_DB_TESTS=1 in CI so a missing database fails the run instead of skipping it silently.
Index ¶
Constants ¶
View Source
const ( // EnvURL points at a Postgres server the tests may create databases on, // for example postgres://postgres:postgres@localhost:5432/postgres. EnvURL = "TEST_DATABASE_URL" // EnvRequire, set to 1, turns a missing EnvURL into a failure instead of // a skip. EnvRequire = "REQUIRE_DB_TESTS" )
Variables ¶
This section is empty.
Functions ¶
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.