1
1
defmodule ExUnit.Callbacks do
2
2
@ moduledoc % B """
3
3
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.
8
42
9
43
## Examples
10
44
11
45
defmodule AssertionTest do
12
46
use ExUnit.Case, async: true
13
47
48
+ # `setup` is called before each test is run
14
49
setup do
15
50
IO.puts "This is a setup callback"
16
51
17
- # Returns extra metadata
52
+ # Return extra metadata, it has to be a keyword list
18
53
{ :ok, [hello: "world"] }
19
54
end
20
55
56
+ # Same as `setup`, but receives the context for the current test
21
57
setup context do
22
- # We can access the test name in the context
58
+ # We can access the test record in the context
23
59
IO.puts "Setting up: #{ context [ :test ] } "
24
60
25
- # The metadata returned by the previous setup as well
61
+ # We can also access the data returned from `setup/0`
26
62
assert context[:hello] == "world"
27
63
28
64
# No metadata
29
65
:ok
30
66
end
31
67
68
+ # This is called after each test finishes
32
69
teardown context do
33
70
assert context[:hello] == "world"
34
71
:ok
@@ -37,6 +74,10 @@ defmodule ExUnit.Callbacks do
37
74
test "always pass" do
38
75
assert true
39
76
end
77
+
78
+ test "another one", context do
79
+ assert context[:hello] == "world"
80
+ end
40
81
end
41
82
42
83
"""
@@ -96,8 +137,8 @@ defmodule ExUnit.Callbacks do
96
137
end
97
138
98
139
@ 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.
101
142
"""
102
143
defmacro teardown ( var // quote ( do: _ ) , block ) do
103
144
quote do
@@ -108,7 +149,8 @@ defmodule ExUnit.Callbacks do
108
149
end
109
150
110
151
@ 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.
112
154
"""
113
155
defmacro setup_all( var // quote ( do: _ ) , block ) do
114
156
quote do
@@ -119,7 +161,7 @@ defmodule ExUnit.Callbacks do
119
161
end
120
162
121
163
@ doc """
122
- Called after the finish of each case .
164
+ Called once after the last test finishes without emitting an :exit message .
123
165
"""
124
166
defmacro teardown_all ( var // quote ( do: _ ) , block ) do
125
167
quote do
@@ -131,6 +173,7 @@ defmodule ExUnit.Callbacks do
131
173
132
174
## Helpers
133
175
176
+ @doc false
134
177
def __merge__ ( _mod , other , :ok ) , do: other
135
178
def __merge__ ( _mod , other , { :ok , data } ) when is_list ( data ) , do: Keyword . merge ( other , data )
136
179
def __merge__ ( mod , _ , failure ) do
0 commit comments