Skip to content

Instantly share code, notes, and snippets.

@moxley
Created March 3, 2023 00:12
Show Gist options
  • Save moxley/d243b4db100373a2b1fd33cdac6082ff to your computer and use it in GitHub Desktop.
Save moxley/d243b4db100373a2b1fd33cdac6082ff to your computer and use it in GitHub Desktop.
Help track down errors and other unwanted output in ExUnit tests when `mix test --trace` isn't sufficient
defmodule TestReporter do
# Why: This is useful for tracking down intermittent errors and output in tests.
# What: Reports the test that ran in the test suite, in the order that they ran, and includes each test's ordinal number
# in the order of the test's finish time.
#
# Usage:
# 1. Save this file to `tests/support` or anywhere it'll get picked up by the Elixir compiler.
# 2. In test_helper.ex, add this GenServer as an ExUnit formater, like this:
# `ExUnit.start(exclude: [:external], formatters: [ExUnit.CLIFormatter, TestReporter])`
# 3. Run the test suite. At the end of the run, it will list all the tests that ran, in the order that they ran.
use GenServer
def start_link(init_args) do
# you may want to register your server with `name: __MODULE__`
# as a third argument to `start_link`
GenServer.start_link(__MODULE__, [init_args])
end
def init(_config) do
{:ok, []}
end
def handle_cast({:test_finished, test}, tests) do
{:noreply, [test | tests]}
end
def handle_cast({:suite_finished, _time}, tests) do
for {test, index} <- Enum.with_index(tests) do
IO.puts("#{index + 1}: #{test.module}: #{test.name}")
end
{:noreply, tests}
end
def handle_cast({_other, _data}, state) do
{:noreply, state}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment