@@ -5,15 +5,25 @@ defmodule ExUnit.Callbacks do
55 This module defines both `setup` and `setup_all` callbacks, as well as
66 the `on_exit/2`, `start_supervised/2` and `stop_supervised/1` functions.
77
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).
1213
1314 The `setup_all` callbacks are invoked only once per module, before any
1415 test runs. All `setup` callbacks are run before each test. No callback
1516 runs if the test case has no tests or all tests have been filtered out.
1617
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+
1727 `start_supervised/2` is used to start processes under a supervisor. The
1828 supervisor is linked to the current test process. The supervisor as well
1929 as all child processes are guaranteed to terminate before any `on_exit/2`
@@ -43,13 +53,10 @@ defmodule ExUnit.Callbacks do
4353
4454 ## Context
4555
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.
5360
5461 Returning `:ok` leaves the context unchanged (both in `setup` and `setup_all`
5562 callbacks).
@@ -132,10 +139,25 @@ defmodule ExUnit.Callbacks do
132139 @ doc """
133140 Defines a callback to be run before each test in a case.
134141
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+
135148 ## Examples
136149
150+ def clean_up_tmp_directory(context) do
151+ # perform setup
152+ :ok
153+ end
154+
137155 setup :clean_up_tmp_directory
138156
157+ setup do
158+ [conn: Plug.Conn.build_conn()]
159+ end
160+
139161 """
140162 defmacro setup ( block ) do
141163 if Keyword . keyword? ( block ) do
@@ -151,6 +173,12 @@ defmodule ExUnit.Callbacks do
151173 @ doc """
152174 Defines a callback to be run before each test in a case.
153175
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+
154182 ## Examples
155183
156184 setup context do
@@ -173,10 +201,25 @@ defmodule ExUnit.Callbacks do
173201 @ doc """
174202 Defines a callback to be run before all tests in a case.
175203
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+
176210 ## Examples
177211
212+ def clean_up_tmp_directory(context) do
213+ # perform setup
214+ :ok
215+ end
216+
178217 setup_all :clean_up_tmp_directory
179218
219+ setup_all do
220+ [conn: Plug.Conn.build_conn()]
221+ end
222+
180223 """
181224 defmacro setup_all ( block ) do
182225 if Keyword . keyword? ( block ) do
@@ -196,6 +239,12 @@ defmodule ExUnit.Callbacks do
196239 @ doc """
197240 Defines a callback to be run before all tests in a case.
198241
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+
199248 ## Examples
200249
201250 setup_all context do
0 commit comments