@@ -5,15 +5,25 @@ defmodule ExUnit.Callbacks do
5
5
This module defines both `setup` and `setup_all` callbacks, as well as
6
6
the `on_exit/2`, `start_supervised/2` and `stop_supervised/1` functions.
7
7
8
- The setup callbacks are defined via macros and each one can optionally
9
- receive a map with metadata, usually referred to as `context`. The context
10
- to be used in the tests can be optionally extended by the callbacks by
11
- returning a properly structured value (see below).
8
+ The setup callbacks are defined via macros and each one can
9
+ optionally receive a map with test state and metadata, usually
10
+ referred to as `context`. The context to be used in the tests can be
11
+ optionally extended by the setup callbacks by returning a properly
12
+ structured value (see below).
12
13
13
14
The `setup_all` callbacks are invoked only once per module, before any
14
15
test runs. All `setup` callbacks are run before each test. No callback
15
16
runs if the test case has no tests or all tests have been filtered out.
16
17
18
+ Both `setup` and `setup_all` can be defined by a block, by passing
19
+ an atom naming a unary function, or by passing a list of such
20
+ atoms. Both can opt to receive the current context by specifying it
21
+ as parameter if defined by a block. Functions used to define a test
22
+ setup must accept the context as single argument.
23
+
24
+ A test module can define mutiple `setup` and `setup_all` callbacks,
25
+ and they are invoked in order of appearance.
26
+
17
27
`start_supervised/2` is used to start processes under a supervisor. The
18
28
supervisor is linked to the current test process. The supervisor as well
19
29
as all child processes are guaranteed to terminate before any `on_exit/2`
@@ -43,13 +53,10 @@ defmodule ExUnit.Callbacks do
43
53
44
54
## Context
45
55
46
- If `setup_all` returns a keyword list, a map, or `{:ok, keywords | map}`,
47
- the keyword list/map will be merged into the current context and will be
48
- available in all subsequent `setup_all`, `setup`, and the `test` itself.
49
-
50
- Similarly, if `setup` returns a keyword list, map, or `{:ok, keywords | map}`,
51
- the returned keyword list/map will be merged into the current context and will
52
- be available in all subsequent `setup` and the `test` itself.
56
+ If `setup_all` or `setup` return a keyword list, a map, or `{:ok,
57
+ keywords | map}`, the keyword list or map will be merged into the
58
+ current context and will be available in all subsequent `setup_all`,
59
+ `setup`, and the `test` itself.
53
60
54
61
Returning `:ok` leaves the context unchanged (both in `setup` and `setup_all`
55
62
callbacks).
@@ -132,10 +139,25 @@ defmodule ExUnit.Callbacks do
132
139
@ doc """
133
140
Defines a callback to be run before each test in a case.
134
141
142
+ Pass a block or name of a unary function as atom, or list of such
143
+ atoms.
144
+
145
+ Can return values to be merged into the context, to set up state for
146
+ tests. See section Context above for details.
147
+
135
148
## Examples
136
149
150
+ def clean_up_tmp_directory(context) do
151
+ # perform setup
152
+ :ok
153
+ end
154
+
137
155
setup :clean_up_tmp_directory
138
156
157
+ setup do
158
+ [conn: Plug.Conn.build_conn()]
159
+ end
160
+
139
161
"""
140
162
defmacro setup ( block ) do
141
163
if Keyword . keyword? ( block ) do
@@ -151,6 +173,12 @@ defmodule ExUnit.Callbacks do
151
173
@ doc """
152
174
Defines a callback to be run before each test in a case.
153
175
176
+ Pass a block or name of a unary function as atom, or list of such
177
+ atoms.
178
+
179
+ Can return values to be merged into the context, to set up state for
180
+ tests. See section Context above for details.
181
+
154
182
## Examples
155
183
156
184
setup context do
@@ -173,10 +201,25 @@ defmodule ExUnit.Callbacks do
173
201
@ doc """
174
202
Defines a callback to be run before all tests in a case.
175
203
204
+ Pass a block or name of a unary function as atom, or list of such
205
+ atoms.
206
+
207
+ Can return values to be merged into the context, to set up state for
208
+ tests. See section Context above for details.
209
+
176
210
## Examples
177
211
212
+ def clean_up_tmp_directory(context) do
213
+ # perform setup
214
+ :ok
215
+ end
216
+
178
217
setup_all :clean_up_tmp_directory
179
218
219
+ setup_all do
220
+ [conn: Plug.Conn.build_conn()]
221
+ end
222
+
180
223
"""
181
224
defmacro setup_all ( block ) do
182
225
if Keyword . keyword? ( block ) do
@@ -196,6 +239,12 @@ defmodule ExUnit.Callbacks do
196
239
@ doc """
197
240
Defines a callback to be run before all tests in a case.
198
241
242
+ Pass a block or name of a unary function as atom, or list of such
243
+ atoms.
244
+
245
+ Can return values to be merged into the context, to set up state for
246
+ tests. See section Context above for details.
247
+
199
248
## Examples
200
249
201
250
setup_all context do
0 commit comments