Skip to content

Instantly share code, notes, and snippets.

@bjfish
Last active April 4, 2016 18:24
Show Gist options
  • Save bjfish/cac0dfbb13038ca60401f02ef92c1d0d to your computer and use it in GitHub Desktop.
Save bjfish/cac0dfbb13038ca60401f02ef92c1d0d to your computer and use it in GitHub Desktop.
diff --git a/lib/ex_unit/lib/ex_unit/cli_formatter.ex b/lib/ex_unit/lib/ex_unit/cli_formatter.ex
index a8f3504..cc72acc 100644
--- a/lib/ex_unit/lib/ex_unit/cli_formatter.ex
+++ b/lib/ex_unit/lib/ex_unit/cli_formatter.ex
@@ -18,7 +18,8 @@ defmodule ExUnit.CLIFormatter do
tests_counter: 0,
failures_counter: 0,
skipped_counter: 0,
- invalids_counter: 0
+ invalids_counter: 0,
+ results: []
}
{:ok, config}
end
@@ -29,6 +30,7 @@ defmodule ExUnit.CLIFormatter do
def handle_event({:suite_finished, run_us, load_us}, config) do
print_suite(config, run_us, load_us)
+ trace_test_times(config)
:remove_handler
end
@@ -43,7 +45,7 @@ defmodule ExUnit.CLIFormatter do
else
IO.write success(".", config)
end
- {:ok, %{config | tests_counter: config.tests_counter + 1}}
+ {:ok, %{config | tests_counter: config.tests_counter + 1, results: [ test | config.results ]}}
end
def handle_event({:test_finished, %ExUnit.Test{state: {:skip, _}} = test}, config) do
@@ -74,7 +76,7 @@ defmodule ExUnit.CLIFormatter do
print_logs(test.logs)
{:ok, %{config | tests_counter: config.tests_counter + 1,
- failures_counter: config.failures_counter + 1}}
+ failures_counter: config.failures_counter + 1, results: [ test | config.results ]}}
end
def handle_event({:case_started, %ExUnit.TestCase{name: name}}, config) do
@@ -97,6 +99,27 @@ defmodule ExUnit.CLIFormatter do
end
## Tracing
+ def trace_test_times(config) do
+ results = Enum.filter(config.results, fn(test) -> test.time > 100_000 end)
+ results = Enum.reverse( Enum.sort_by(results, fn(test) -> test.time end) )
+ length_longest_name = Enum.reduce(results, 0, fn(test, acc) ->
+ len = String.length(test_case_name(test))
+ if len > acc, do: len, else: acc
+ end)
+ longest_time = Enum.reduce(results, 0, fn(test, acc) ->
+ len = String.length(trace_test_time(test))
+ if len > acc, do: len, else: acc
+ end)
+
+ Enum.each( results, fn(test) ->
+ IO.puts(String.ljust(test_case_name(test), length_longest_name + 1) <>
+ String.rjust(trace_test_time(test), longest_time))
+ end)
+ end
+
+ defp test_case_name(%ExUnit.Test{case: case } = test) do
+ "#{inspect case} #{trace_test_name test}"
+ end
defp trace_test_name(%ExUnit.Test{name: name}) do
case Atom.to_string(name) do
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment