Packages
testcontainers
1.2.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)
## Prerequisites
Before 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](https://www.docker.com/products/docker-desktop) installed and running
- You are familiar with Elixir and Docker basics
## Installation
To add Testcontainers to your project, follow these steps:
1. Add `testcontainers` to your list of dependencies in `mix.exs`:
```elixir
def deps do
[
{:testcontainers, "~> x.x.x"}
]
end
```
2. Run mix deps.get
3. Add the following to test/test_helper.exs
```elixir
Testcontainers.start_link()
```
## Usage
This section explains how to use the Testcontainers library in your own project.
### In a Phoenix project:
In simple terms you can add this in application.ex:
```elixir
# In your application.ex file in your Phoenix project:
import Testcontainers.Ecto
@impl true
def start(_type, _args) do
if Mix.env() == :test,
do:
postgres_container(
app: :my_app,
user: "postgres",
password: "postgres"
)
# .. other setup code
end
```
see documentation on Testcontainers.Ecto for more information about the options it can take.
There is an example repo here with a bare bones phoenix application, where the only changes are the use of the ecto function and removing the test alias that interferes with it:
[https://github.com/jarlah/hello_testcontainers](https://github.com/jarlah/hello_testcontainers)
There is also another example repo without Phoenix, just a bare mix project, which show cases that the ecto dependencies are in fact optional:
[https://github.com/jarlah/mix_teststcontainers](https://github.com/jarlah/mix_teststcontainers)
### In your ExUnit tests
Here's a simple example of how to use a MySQL container in your ExUnit tests:
```elixir
# test/simple_mysql_container_test.exs
defmodule SimpleMySqlContainerTest do
use ExUnit.Case, async: true
import Testcontainers.ExUnit
alias Testcontainers.Container.MySqlContainer
describe "with default configuration" do
container(:mysql, MySqlContainer.new())
test "provides a ready-to-use mysql container", %{mysql: mysql} do
assert mysql.environment[:MYSQL_MAJOR] == "8.0"
end
end
end
```
You can also define a shared container for all tests in a test module:
```elixir
# test/shared_mysql_container_test.exs
defmodule SharedMySqlContainerTest do
use ExUnit.Case, async: true
import Testcontainers.ExUnit
alias Testcontainers.Container.MySqlContainer
container(:mysql, MySqlContainer.new(), shared: true)
describe "with default configuration" do
test "provides a ready-to-use mysql container", %{mysql: mysql} do
assert mysql.environment[:MYSQL_MAJOR] == "8.0"
end
test "does something else", %{mysql: mysql} do
# ....
end
end
end
```
In the last example only one container will be created and started.
### Logging
By default, Testcontainers doesn't log anything. If you want Testcontainers to log, set the desired log level in config/test.exs:
```elixir
# config/test.exs
import Config
config :testcontainers,
log_level: :warning
```
## API Documentation
For 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).
## Contributing
We welcome your contributions! Please see our contributing guidelines (TBD) for more details on how to submit patches and the contribution workflow.
## License
Testcontainers is available under the MIT license. See the LICENSE file for more info.
## Contact
If you have any questions, issues, or want to contribute, feel free to contact us.
---
Thank you for using Testcontainers to test your Elixir applications!