Testcontainers starts containerized dependencies for tests. An Order service database adapter can be checked against a running MySQL instance rather than a mock, helping reveal failures caused by code or database-version changes. section 7.3
For a Go database-adapter test:
- Add testcontainers-go and create a testify suite that holds the database connection URL. section 7.3.2
- In SetupSuite, request a MySQL container with its image, port, environment, and SQL readiness check; give its available endpoint to the adapter. section 7.3.2
- Run Save and Get tests against that adapter, then tear down shared resources after the suite. Use a nonroot database user in preference to the root user shown in the test setup. section 7.3.1 section 7.3.2
Checking an order round trip
Save an order containing a product item, retrieve it by ID, and assert that the returned customer ID matches the original. This checks the adapter’s database interaction, not just its behavior in isolation. section 7.3.2
The same suite lifecycle can support an end-to-end test. LocalDockerCompose starts the application stack in SetupSuite; a gRPC client calls the Order service’s Create and Get endpoints; TearDownSuite brings the stack down, including when the test fails. section 7.4.7
func (c *CreateOrderTestSuite) SetupSuite() {
composeFilePaths := []string{"resources/docker-compose.yml"} #1
identifier := strings.ToLower(uuid.New().String()) #2
compose := testcontainers.NewLocalDockerCompose(composeFilePaths,
identifier)
c.compose = compose #3
execError := compose.
WithCommand([]string{"up", "-d"}).
Invoke() #4
err := execError.Error
if err != nil {
log.Fatalf("Could not run compose stack: %v", err)
}
}
The setup listing shows how a suite starts its Docker Compose stack before the gRPC test runs. section 7.4.7




