liveBook · Manning

Testcontainers

As explained in gRPC Microservices in Go

Testcontainers is a library ecosystem for programmatically creating and managing disposable containerized dependencies during tests.

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

How gRPC Microservices in Go covers it

How deep it goes
Works through suite setup, container readiness, endpoint use, assertions, and stack teardown without examining Testcontainers internals.
Examples
Uses Order service Save and Get assertions against MySQL and a gRPC Create and Get flow against a Compose stack.
What it assumes
Introduces testify suite hooks and dependency installation while assuming familiarity with Go tests, database adapters, and containerized services.
What you can do after
Apply it via testcontainers-go to test an adapter against MySQL and use LocalDockerCompose to start and stop a gRPC test stack.
How it connects
Connects integration testing and database adapters to MySQL, then extends the suite pattern to Docker Compose, gRPC, and end-to-end testing.

Also explained in

Compare how 5 books explain Testcontainers →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of gRPC Microservices in Go. The section links point to the parts of the book it draws on.