11defmodule ExUnit.Callbacks do
22 @ moduledoc % B """
33 This module defines four callbacks: `setup_all`, `teardown_all`,
4- `setup` and `teardown`. Those callbacks are defined via macros
5- and receives a keyword list of metadata. The callback may
6- optionally define extra data which will be available in the test
7- cases.
4+ `setup` and `teardown`.
5+
6+ Those callbacks are defined via macros and each one can optionally receive a
7+ keyword list with metadata, usually referred to as `context`. The callback
8+ may optionally put extra data into `context` to be used in the tests.
9+
10+ **Note**: `setup` and `teardown` callbacks share the same context, it
11+ provides an ExUnit.Test record associated with the `:test` key. `setup_all`
12+ and `teardown_all` share their own context in a similar way, but this one
13+ provides an ExUnit.TestCase record associated with the `:case` key.
14+
15+ If you return { :ok, <keyword list> } from `setup` or `teardown`, the keyword
16+ list will get merged into the context that will be available in all
17+ subsequent `setup`, `test`, or `teardown` calls.
18+
19+ Similarly, returning { :ok, <keyword list> } from `setup_all` or
20+ `teardown_all` will merge the keyword list into the context that will be
21+ available in all subsequent `setup_all` or `teardown_all` calls.
22+
23+ Returning :ok leaves the context unchanged in both cases.
24+
25+ Returning anything else from `setup` or `teardown` will force the current
26+ test to fail, and subsequent `setup`, `test`, and `teardown` callbacks won't
27+ be called for it.
28+
29+ Returning anything else from `setup_all` or `teardown_all` will force the
30+ whole case to fail, and no other callback will be called.
31+
32+ It is allowed to define multiple `setup` or `teardown` callbacks, they will
33+ be called sequentially in the order of definition before each test. The
34+ returned keyword list from the last `setup` will be merged into the context passed to
35+ the `test` and `teardown` (if defined) callbacks.
36+
37+ In the case of `setup_all` and `teardown_all` callbacks, each `setup_all`
38+ will be called only once before the first test's `setup` and each
39+ `teardown_all` will be called once after the last test. The returned keyword
40+ list from the last `setup_all` will get merged into the context passed to the
41+ `teardown_all` callbacks.
842
943 ## Examples
1044
1145 defmodule AssertionTest do
1246 use ExUnit.Case, async: true
1347
48+ # `setup` is called before each test is run
1449 setup do
1550 IO.puts "This is a setup callback"
1651
17- # Returns extra metadata
52+ # Return extra metadata, it has to be a keyword list
1853 { :ok, [hello: "world"] }
1954 end
2055
56+ # Same as `setup`, but receives the context for the current test
2157 setup context do
22- # We can access the test name in the context
58+ # We can access the test record in the context
2359 IO.puts "Setting up: #{ context [ :test ] } "
2460
25- # The metadata returned by the previous setup as well
61+ # We can also access the data returned from `setup/0`
2662 assert context[:hello] == "world"
2763
2864 # No metadata
2965 :ok
3066 end
3167
68+ # This is called after each test finishes
3269 teardown context do
3370 assert context[:hello] == "world"
3471 :ok
@@ -37,6 +74,10 @@ defmodule ExUnit.Callbacks do
3774 test "always pass" do
3875 assert true
3976 end
77+
78+ test "another one", context do
79+ assert context[:hello] == "world"
80+ end
4081 end
4182
4283 """
@@ -96,8 +137,8 @@ defmodule ExUnit.Callbacks do
96137 end
97138
98139 @ doc """
99- Called after the finish of each test. Note that, if the test crasches with an exit
100- message `teardown` will not be run.
140+ Called after the finish of each test. Note that if the test crashed with an : exit
141+ message, `teardown` will not be run.
101142 """
102143 defmacro teardown ( var // quote ( do: _ ) , block ) do
103144 quote do
@@ -108,7 +149,8 @@ defmodule ExUnit.Callbacks do
108149 end
109150
110151 @ doc """
111- Called before the start of a case.
152+ Called before the start of a case, i.e. called once before the first test in
153+ the current module and before any `setup` callbacks.
112154 """
113155 defmacro setup_all( var // quote ( do: _ ) , block ) do
114156 quote do
@@ -119,7 +161,7 @@ defmodule ExUnit.Callbacks do
119161 end
120162
121163 @ doc """
122- Called after the finish of each case .
164+ Called once after the last test finishes without emitting an :exit message .
123165 """
124166 defmacro teardown_all ( var // quote ( do: _ ) , block ) do
125167 quote do
@@ -131,6 +173,7 @@ defmodule ExUnit.Callbacks do
131173
132174 ## Helpers
133175
176+ @doc false
134177 def __merge__ ( _mod , other , :ok ) , do: other
135178 def __merge__ ( _mod , other , { :ok , data } ) when is_list ( data ) , do: Keyword . merge ( other , data )
136179 def __merge__ ( mod , _ , failure ) do
0 commit comments