Skip to content

Commit 0b77a54

Browse files
author
José Valim
committed
Merge pull request #991 from ericmj/exunit_exit
Use monitors in ExUnit and document callbacks
2 parents cebbbd5 + 7d1a763 commit 0b77a54

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

Diff for: lib/ex_unit/lib/ex_unit/callbacks.ex

+13
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ defmodule ExUnit.Callbacks do
7979
compile_callbacks(env, :exunit_teardown_all) ]
8080
end
8181

82+
@doc """
83+
Called before the start of each test.
84+
"""
8285
defmacro setup(var // quote(do: _), block) do
8386
quote do
8487
name = :"__exunit_setup_#{length(@exunit_setup)}"
@@ -87,6 +90,10 @@ defmodule ExUnit.Callbacks do
8790
end
8891
end
8992

93+
@doc """
94+
Called after the finish of each test. Note that, if the test crasches with an exit
95+
message `teardown` will not be run.
96+
"""
9097
defmacro teardown(var // quote(do: _), block) do
9198
quote do
9299
name = :"__exunit_teardown_#{length(@exunit_teardown)}"
@@ -95,6 +102,9 @@ defmodule ExUnit.Callbacks do
95102
end
96103
end
97104

105+
@doc """
106+
Called before the start of a case.
107+
"""
98108
defmacro setup_all(var // quote(do: _), block) do
99109
quote do
100110
name = :"__exunit_setup_all_#{length(@exunit_setup_all)}"
@@ -103,6 +113,9 @@ defmodule ExUnit.Callbacks do
103113
end
104114
end
105115

116+
@doc """
117+
Called after the finish of each case.
118+
"""
106119
defmacro teardown_all(var // quote(do: _), block) do
107120
quote do
108121
name = :"__exunit_teardown_all_#{length(@exunit_teardown_all)}"

Diff for: lib/ex_unit/lib/ex_unit/runner.ex

+2-3
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,8 @@ defmodule ExUnit.Runner do
8989
config.formatter.test_started(config.formatter_id, test)
9090

9191
# Run test in a new process so that we can trap exits for a single test
92-
Process.flag(:trap_exit, true)
9392
self_pid = self
94-
test_pid = spawn_link fn ->
93+
{ test_pid, test_ref } = Process.spawn_monitor fn ->
9594
test = try do
9695
context = test_case.__exunit__(:setup, Keyword.put(context, :test, test))
9796

@@ -122,7 +121,7 @@ defmodule ExUnit.Runner do
122121
receive do
123122
{ ^test_pid, :test_finished, test } ->
124123
pid <- { test_pid, :test_finished, test }
125-
{ :EXIT, ^test_pid, { error, stacktrace } } ->
124+
{ :DOWN, ^test_ref, :process, ^test_pid, { error, stacktrace } } ->
126125
test = test.failure { :EXIT, error, filter_stacktrace(stacktrace) }
127126
pid <- { test_pid, :test_finished, test }
128127
end

0 commit comments

Comments
 (0)