Testcontainers and container configuration setup for ExTest

Hi, I’m a longtime JVM language developer getting started with my first real project destined for production using Elixir.

As a longtime JVM developer, I was delighted to see that Devcontainers has an implementation for Elixir…in the JVM world it’s been a very useful tool for me when writing tests that incorporate (real) external resources.

But I’m having one problem…I think I’d like the testcontainer to be defined in the ExTest test file, but have the container started in some way before the “main” Application is started and test execution occurs so that it can set test environment configs (like container connection properties) to be picked up during the “main” Application is started-where those configs will be used).

Is there some slight-of-hand way I could do this? I’ve included some code, with the block magically_executed_before_the_world_begins standing in as the construct I’d like to have, if possible.


defmodule ProjectTest do
  use ExUnit.Case
  import Testcontainers.ExUnit

  container(
    :cassandra,
    Testcontainers.CassandraContainer.new()
  )

  magically_executed_before_the_world_begins do
    # is there some way to do this?

    import Config

    config :cassandra,
      host: Testcontainers.Container.get_host(:cassandra),
      port: Testcontainers.Container.get_host_port(:cassandra, 9042)
  end

  setup_all do
    # setup_all seems to run before tests but after the
    # Application under test has been started so it won't do the trick
    :ok
  end

  test "greets the world" do
    # here we can then use the Cassandra connection, initialized through
    # `Application.start` to test our application

thanks for your feedback

for future users who may encounter this, I ended up solving this problem by using DynamoDB(and ExAws) rather than Cassandra.

But…

I think I could have solved this problem by stopping and then starting the application in a setup block after setting the connection information needed in config(and of course having that config picked up at runtime):

from within a ExUnit test module:

 setup do
    :ok = Application.stop(:your_app_name)
    :ok = Application.start(:you_app_name)
  end