home  .   articles  .   til  .   about  .   contact

IEx Test Helper

This week at Chicago Elixir we were paring on some Exercism. Dorian Karter was looking for a way to run ExUnit tests inside of IEx without mix. Surely there must be a way! After some research I came up with this nugget.

#.iex.exs
defmodule TestHelper do
  @moduledoc false

  @doc"""
  Reloads files and runs ExUnit tests in the pwd
  """
  def run_tests(opts \\ []) do
    respawn

    clear? = Keyword.get(opts, :clear, false)
    if clear?, do: clear

    Code.compiler_options(ignore_module_conflict: true)
    Path.wildcard("*_test.exs")
    |> Enum.map(& Code.load_file &1)

    ExUnit.Server.cases_loaded
    ExUnit.run
  end
end

This assumes that the *_test.exs files in the pwd load the required modules first. Just plop this file in the pwd or in your home directory to obtain access to it in IEx. Happy testing!