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.


Join the 80/20 DevOps Newsletter

If you're an engineering leader or developer, you should subscribe to my 80/20 DevOps Newsletter. Give me 1 minute of your day, and I'll teach you essential DevOps skills. I cover topics like Kubernetes, AWS, Infrastructure as Code, and more.

Not sure yet? Check out the archive.

Unsubscribe at any time.