Testcontainers for Integration Tests

If you’re writing code that depends on another service, such as a database, you might consider using Testcontainers.

Testcontainers is a library that hooks into your language’s test suite and abstracts away spinning up containers as part of the test run.

For example, if you’re writing a Ruby on Rails application that depends on Redis, you can have it spin up a Redis docker container as part of your testing cycle like so:

redis = Testcontainers::DockerContainer.new("redis:5.0.3-alpine").with_exposed_port(6379)
redis.start

I’ve used Testcontainers at a past company. It helped us reduce the number of mocks needed and gave us more confidence because we tested against real services.

One potential trap you might want to plan for is parameterizing your container’s URLs and ports. Otherwise, you’ll run into issues with parallelizing your tests in the future – you can’t have multiple Redis containers with the same port at the same time.


Master GitHub Actions with a Senior Infrastructure Engineer

As a senior staff infrastructure engineer, I share exclusive, behind-the-scenes insights that you won't find anywhere else. Get the strategies and techniques I've used to save companies $500k in CI costs and transform teams with GitOps best practices—delivered straight to your inbox.

Not sure yet? Check out the archive.

Unsubscribe at any time.