I am looking for a smart way to reuse tests.
My project Raxx is a common interface for web servers. So far I have created adapter implementations for two servers cowboy and elli. I am now working an a third adapter for Ace and want to reuse some of my test suits.
For example I have to test the elli adapter.
# test/raxx/adapters/elli/request_test.exs
defmodule Raxx.Elli.RequestTest do
use ExUnit.Case, async: true
setup do
{:ok, _pid} = :elli.start_link [
callback: Raxx.Adapters.Elli.Handler,
callback_args: {Raxx.TestSupport.Forwarder, %{target: self()}},
port: 2020]
{:ok, %{port: 2020}}
end
test "request shows correct method", %{port: port} do
{:ok, _resp} = HTTPoison.get("localhost:#{port}")
assert_receive %{method: "GET"}
end
# more tests
end
and similar for the cowboy adapter.
defmodule Raxx.Cowboy.RequestTest do
use ExUnit.Case, async: true
setup %{case: case, test: test} do
name = {case, test}
routes = [
{:_, Raxx.Adapters.Cowboy.Handler, {Raxx.TestSupport.Forwarder, %{target: self()}}}
]
dispatch = :cowboy_router.compile([{:_, routes}])
env = [dispatch: dispatch]
{:ok, _pid} = :cowboy.start_http(name, 2, [port: 0], [env: env])
{:ok, %{port: :ranch.get_port(name)}}
end
test "request shows correct host", %{port: port} do
{:ok, _resp} = HTTPoison.get("localhost:#{port}")
assert_receive %{host: "localhost"}
end
end
The test suit will be the same for all adapters and only the setup code will vary between them. Is there a smart way to reuse the test suits for different setups?