https://github.com/nhatthm/testcontainers-go-extra
Boost testcontainers/testcontainers-go with some jet fuel! ๐
https://github.com/nhatthm/testcontainers-go-extra
container docker go golang test testcontainers testcontainers-go
Last synced: 3 months ago
JSON representation
Boost testcontainers/testcontainers-go with some jet fuel! ๐
- Host: GitHub
- URL: https://github.com/nhatthm/testcontainers-go-extra
- Owner: nhatthm
- License: mit
- Created: 2022-01-27T17:35:33.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2025-08-07T18:17:00.000Z (10 months ago)
- Last Synced: 2025-08-07T19:26:29.426Z (10 months ago)
- Topics: container, docker, go, golang, test, testcontainers, testcontainers-go
- Language: Go
- Homepage:
- Size: 294 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Testcontainers-Go Booster
[](https://github.com/nhatthm/testcontainers-go-extra/releases/latest)
[](https://github.com/nhatthm/testcontainers-go-extra/actions/workflows/test.yaml)
[](https://codecov.io/gh/nhatthm/testcontainers-go-extra)
[](https://goreportcard.com/report/go.nhat.io/testcontainers-extra)
[](https://pkg.go.dev/go.nhat.io/testcontainers-extra)
[](https://www.paypal.com/donate/?hosted_button_id=PJZSGJN57TDJY)
Boost `testcontainers/testcontainers-go` with some jet fuel! ๐
## Prerequisites
- `Go >= 1.23`
## Install
```bash
go get go.nhat.io/testcontainers-extra
```
## Callbacks
After successfully starting a container, you may want to do some extra operations to make it ready for your purposes.
Then you could use the callbacks.
For example:
```go
package example
import (
"context"
"time"
"go.nhat.io/testcontainers-extra"
"go.nhat.io/testcontainers-extra/wait"
)
func startPostgres(dbName, dbUser, dbPassword string) error {
_, err := testcontainers.StartGenericContainer(context.Background(), testcontainers.ContainerRequest{
Name: "postgres",
Image: "postgres:12-alpine",
ExposedPorts: []string{":5432"},
Env: map[string]string{
"LC_ALL": "C.UTF-8",
"POSTGRES_DB": dbName,
"POSTGRES_USER": dbUser,
"POSTGRES_PASSWORD": dbPassword,
},
WaitingFor: wait.ForHealthCheckCmd("pg_isready").
WithRetries(3).
WithStartPeriod(30 * time.Second).
WithTestTimeout(5 * time.Second).
WithTestInterval(10 * time.Second),
}, testcontainers.WithCallback(func(ctx context.Context, c testcontainers.Container, r testcontainers.ContainerRequest) error {
// Do your stuff here, for example, migration.
return nil
}))
return err
}
```
### Populating Host and Ports Envs
`testcontainers.PopulateHostPortEnv` is a callback that set the environment variables for the exposed ports.
For example:
```go
package example
import (
"context"
"time"
"go.nhat.io/testcontainers-extra"
"go.nhat.io/testcontainers-extra/wait"
)
func startPostgres(dbName, dbUser, dbPassword string) error {
_, err := testcontainers.StartGenericContainer(context.Background(), testcontainers.ContainerRequest{
Name: "postgres",
Image: "postgres:12-alpine",
ExposedPorts: []string{":5432"},
Env: map[string]string{
"LC_ALL": "C.UTF-8",
"POSTGRES_DB": dbName,
"POSTGRES_USER": dbUser,
"POSTGRES_PASSWORD": dbPassword,
},
WaitingFor: wait.ForHealthCheckCmd("pg_isready").
WithRetries(3).
WithStartPeriod(30 * time.Second).
WithTestTimeout(5 * time.Second).
WithTestInterval(10 * time.Second),
}, testcontainers.PopulateHostPortEnv)
return err
}
```
After calling `startPostgres()`, there will be 2 variables `POSTGRES_5432_HOST` and `POSTGRES_5432_PORT`. The `POSTGRES`
is from the container name in the request, `5432` is the exposed port. The values are
- `POSTGRES_5432_HOST`: the hostname of the docker daemon where the container port is exposed.
- `POSTGRES_5432_PORT`: the port that mapped to the exposed container port.
## Wait Strategies
### Health Check
The health check provides the same behavior as `docker-compose` with the configuration:
- `Start Period`: Retry is only counted when time passes the start period. This is helpful for some containers that need
time to get ready. The default value is `0`.
- `Test Timeout`: Timeout for executing the test.
- `Test Internal`: If the container is unhealthy, the health check will wait for an amount of time before testing again.
- `Retries`: The number of retries to test the container after start period ends.

For example:
```go
package example
import (
"context"
"time"
"go.nhat.io/testcontainers-extra"
"go.nhat.io/testcontainers-extra/wait"
)
func startPostgres(dbName, dbUser, dbPassword string) error {
_, err := testcontainers.StartGenericContainer(context.Background(), testcontainers.ContainerRequest{
Name: "postgres",
Image: "postgres:12-alpine",
ExposedPorts: []string{":5432"},
Env: map[string]string{
"LC_ALL": "C.UTF-8",
"POSTGRES_DB": dbName,
"POSTGRES_USER": dbUser,
"POSTGRES_PASSWORD": dbPassword,
},
WaitingFor: wait.ForHealthCheckCmd("pg_isready").
WithRetries(3).
WithStartPeriod(30 * time.Second).
WithTestTimeout(5 * time.Second).
WithTestInterval(10 * time.Second),
})
return err
}
```
## Donation
If this project help you reduce time to develop, you can give me a cup of coffee :)
### Paypal donation
[](https://www.paypal.com/donate/?hosted_button_id=PJZSGJN57TDJY)
ย ย ย ย ย ย ย or scan this