Compox ensure your whole infrastructure is up before running the tests.
Compox helps you to setup an ephemeral environment to run your tests using the
docker-compose.yml
present in your project. After all your tests are done,
it will take the test environment down. This way your machine won't waste
resources having the containers running when they are not needed.
- Reuse created/running containers for other projects (e.g. shared database)
- Automatic start of the required container services
- Automatic stop of the required container services
Adding compox
to your list of dependencies in mix.exs
:
def deps do
[
{:compox, "~> 0.1.0-rc3", only: [:test]}
]
end
To use Compox follow these steps:
-
Create a
docker-compose.yml
file with the containers that will be started by this application. -
Ensure Compox starts all containers before running the tests:
defp aliases do
[
test: ["compox.up", "ecto.create --quiet", "ecto.migrate --quiet", "test"]
]
end
- Edit your
test_helper.exs
to stop the containers after the tests are done:
ExUnit.after_suite(fn _ ->
Compox.stop()
end)
Sometimes a container is started by it takes a while to be ready to be used by your application. For example, a database container can be started by the database will take few seconds to start accepting connections.
To avoid any errors, Compox supports upchecks probe configurations to validate whether a container can be used or not.
You can implement your own logic to validate if a container is ready (see example below) or use the default provided upchecks:
Postgres
:Compox.Upchecks.Postgrex
Compox allows you to configure a project to start/stop the containers to run the tests, but also provides configuration to work with any kind of situation.
For example, some of or coworkers prefer to have the containers running all the time. In this case you want to ensure the docker containers are started before the test, so people that prefer to save resources will start the containers, but you will also won't to avoid to take containers down.
You config/test.exs
file will look like:
use Mix.Config
config :compox,
auto_start: true,
auto_stop: false,
exclude: ["a_service_should_not_be_started"],
container_upchecks: [
"postgres-service": {Compox.Upchecks.Postgrex, []},
another_service: fn ->
Another.Connection.up?()
end
]
The people who will want to stop the containers after test can use a dotenv
library to change the auto_stop
config to true
on their machines.
This can work the other way around, start/stopping the containers always and having
dotfile
config to skipping the start/stop.