Reusing a test suit for Adapter implementations

I have a project where there are a series of adapters. Each adapter should have the same public behaviour.
It would be neat if I could reuse a test suite multiple times.

For example.

Set up tests that check an adapter fulfils a contract.

defmodule MyServiceContract do

  test "can fetch a user by id", %{service: service} do
    user_id = :user
    assert %{id: user_id} = MyService.fetch(service, user_id)
  end
end

Then use the test suit in multiple implementations.

defmodule MyService.ProviderFooAdapterTest do
  use MyServiceContract

  setup %{} do
    adapter = # setup adapter
    {:ok, %{service: adapter}}
  end
end
defmodule MyService.RealAdapterTest do
  use MyServiceContract

  setup %{} do
    adapter = # setup adapter
    {:ok, %{service: adapter}}
  end
end

I have tried looking at ExUnit.CaseTemplate But this seams to be for the usecase where you share setup and write individual tests

2 Likes