Skip to main content

RunTestMain

RunTestMain starts an embedded PostgreSQL instance, runs migrate, executes m.Run(), and returns an exit code suitable for os.Exit. The connected *sqlx.DB is written to db. Intended for use in TestMain functions:

def RunTestMain(
m: *testing.M,
port: uint32,
dbName: string,
db: **sqlx.DB,
migrate: func(*sqlx.DB) error
) - > int

Starts an embedded PostgreSQL instance, runs database migrations, executes the provided testing.M's Run() method, and then stops the PostgreSQL instance. This function is intended for use in Go TestMain functions to set up and tear down a database for integration tests.

Parameters

NameTypeDescription
m*testing.MThe testing.M instance provided by the Go test runner, used to execute the tests.
portuint32The port number on which the embedded PostgreSQL instance will listen.
dbNamestringThe name of the database to be created and used within the embedded PostgreSQL instance.
db**sqlx.DBA pointer to a *sqlx.DB pointer. The connected *sqlx.DB instance will be written to this location for use by the tests.
migratefunc(*sqlx.DB) errorA function that takes a *sqlx.DB connection and applies necessary database migrations. This function is executed after the database is started and connected, but before tests are run.

Returns

TypeDescription
intAn exit code suitable for os.Exit, typically 0 for success or 1 for failure.