Packages
testcontainers
1.10.4
2.3.1
2.3.0
2.2.0
2.1.0
2.1.0-rc1
2.0.0
2.0.0-rc3
2.0.0-rc2
2.0.0-rc
1.14.1
1.14.0
1.13.5
1.13.4
1.13.3
1.13.2
1.13.1
1.13.0
1.12.0
1.11.8
1.11.7
1.11.6
1.11.5
1.11.4
1.11.3
1.11.2
1.11.1
1.11.0
1.10.5
1.10.4
1.10.3
1.10.2
1.10.1
1.10.0
1.9.0
1.8.4
1.8.3
1.8.2
1.8.1
1.8.0
1.7.0
1.6.0
1.5.1
1.5.0
1.4.2
1.4.1
1.4.0
1.3.1
1.3.0
1.2.11
1.2.10
1.2.9
1.2.8
1.2.7
1.2.6
1.2.5
1.2.4
1.2.3
1.2.2
1.2.1
1.2.0
1.1.3
1.1.2
1.1.1
1.1.0
1.0.1
1.0.0
1.0.0-beta.1
1.0.0-beta
0.9.3
0.9.2
0.9.1
0.9.0
Testcontainers is an Elixir library that supports ExUnit tests, providing lightweight, throwaway instances of common databases, Selenium web browsers, or anything else that can run in a Docker container.
Current section
Files
Jump to
Current section
Files
testcontainers
README.md
README.md
# Testcontainers[](https://hex.pm/packages/testcontainers)> Testcontainers is an Elixir library that supports ExUnit tests, providing lightweight, throwaway instances of common databases, Selenium web browsers, or anything else that can run in a Docker container.## Table of Contents- [Prerequisites](#prerequisites)- [Installation](#installation)- [Usage](#usage)- [API Documentation](#api-documentation)- [Contributing](#contributing)- [License](#license)- [Contact](#contact)## PrerequisitesBefore you begin, ensure you have met the following requirements:- You have installed the latest version of [Elixir](https://elixir-lang.org/install.html)- You have a Docker runtime installed- You are familiar with Elixir and Docker basics## InstallationTo add Testcontainers to your project, follow these steps:1. Add `testcontainers` to your list of dependencies in `mix.exs`:```elixirdef deps do [ {:testcontainers, "~> 1.10"} ]end```2. Run mix deps.get3. Add the following to test/test_helper.exs```elixirTestcontainers.start_link()```## UsageThis section explains how to use the Testcontainers library in your own project.### Basic usageYou can use generic container api, where you have to define everything yourself:```elixir{:ok, _} = Testcontainers.start_link()config = %Testcontainers.Container{image: "redis:5.0.3-alpine"}{:ok, container} = Testcontainers.start_container(config)```Or you can use one of many predefined containers like `RedisContainer`, that has waiting strategies among other things defined up front with good defaults:```elixir{:ok, _} = Testcontainers.start_link()config = Testcontainers.RedisContainer.new(){:ok, container} = Testcontainers.start_container(config)```If you want to use a predefined container, such as `RedisContainer`, with an alternative image, for example, `valkey/valkey`, it's possible:```elixir{:ok, _} = Testcontainers.start_link()config = Testcontainers.RedisContainer.new() |> Testcontainers.RedisContainer.with_image("valkey/valkey:latest") |> Testcontainers.RedisContainer.with_check_image("valkey/valkey"){:ok, container} = Testcontainers.start_container(config)```### ExUnit testsGiven you have added Testcontainers.start_link() to test_helper.exs:```elixirsetup config = Testcontainers.RedisContainer.new() {:ok, container} = Testcontainers.start_container(config) ExUnit.Callbacks.on_exit(fn -> Testcontainers.stop_container(container.container_id) end) {:ok, %{redis: container}}end```there is a macro that can simplify this down to a oneliner:```elixirimport Testcontainers.ExUnitcontainer(:redis, Testcontainers.RedisContainer.new())```### Run tests in a Phoenix project (or any project for that matter)To run/wrap testcontainers around a project use the testcontainers.test task.`mix testcontainers.test [--database postgres|mysql]`to use postgres you can just run`mix testcontainers.test` since postgres is default.in your config/test.exs you can then change the repo config to this:```config :my_app, MyApp.Repo, username: System.get_env("DB_USER") || "postgres", password: System.get_env("DB_PASSWORD") || "postgres", hostname: System.get_env("DB_HOST") || "localhost", port: System.get_env("DB_PORT") || "5432", database: "my_app_test#{System.get_env("MIX_TEST_PARTITION")}", pool: Ecto.Adapters.SQL.Sandbox, pool_size: System.schedulers_online() * 2```Activate reuse of database containers started by mix task with adding `testcontainers.reuse.enable=true` in `~/.testcontainers.properties`. This is experimental.### LoggingBy default, Testcontainers doesn't log anything. If you want Testcontainers to log, set the desired log level in config/test.exs:```elixir# config/test.exsimport Config config :testcontainers, log_level: :warning```## API DocumentationFor more detailed information about the API, different container configurations, and advanced usage scenarios, please refer to the [API documentation](https://hexdocs.pm/testcontainers/api-reference.html).## ContributingWe welcome your contributions! Please see our contributing guidelines (TBD) for more details on how to submit patches and the contribution workflow.## LicenseTestcontainers is available under the MIT license. See the LICENSE file for more info.## ContactIf you have any questions, issues, or want to contribute, feel free to contact us.---Thank you for using Testcontainers to test your Elixir applications!