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 from the TestMain function, used to run the actual 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 variable where the connected database instance will be written, allowing tests to access the database connection.
migratefunc(*sqlx.DB) errorA function that takes a *sqlx.DB connection and applies necessary database migrations or schema setup before tests are run.

Returns

TypeDescription
intAn exit code suitable for os.Exit, indicating the success or failure of the test run.