Skip to content

Commit 8836405

Browse files
author
José Valim
committed
Merge pull request #1201 from alco/exunit-docs
Extend ExUnit docs
2 parents a6f04a2 + dd2e0f6 commit 8836405

File tree

3 files changed

+89
-20
lines changed

3 files changed

+89
-20
lines changed

lib/ex_unit/lib/ex_unit.ex

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,19 @@ defmodule ExUnit do
1616
end
1717

1818
@moduledoc """
19-
Basic unit test structure for Elixir.
19+
Basic unit testing framework for Elixir.
2020
2121
## Example
2222
2323
A basic setup for ExUnit is shown below:
2424
2525
# File: assertion_test.exs
2626
27-
# 1) Start ExUnit. You can pass some options as argument (list below)
27+
# 1) Start ExUnit. You could also pass some options to the start function
28+
# (see `configure/1` for the list of options)
2829
ExUnit.start
2930
30-
# 2) Next we create a new TestCase and use ExUnit.Case
31+
# 2) Create a new test module (or "case") and use ExUnit.Case
3132
defmodule AssertionTest do
3233
# 3) Notice we pass async: true, this runs the test case
3334
# concurrently with other test cases
@@ -45,20 +46,42 @@ defmodule ExUnit do
4546
end
4647
end
4748
48-
To run the test above, all you need to to is to run the file
49+
To run the test above, all you need to do is to run the file
4950
using elixir from command line. Assuming you named your file
5051
assertion_test.exs, you can run it as:
5152
5253
bin/elixir assertion_test.exs
5354
5455
## Case, callbacks and assertions
5556
56-
Check `ExUnit.Case` and `ExUnit.Callbacks` for more information about
57+
See `ExUnit.Case` and `ExUnit.Callbacks` for more information about
5758
defining test cases.
5859
5960
The `ExUnit.Assertions` module contains a set of macros to easily
6061
generate assertions with appropriate error messages.
6162
63+
## Integration with Mix
64+
65+
Mix is the project management and build tool for Elixir. Invoking `mix test`
66+
from the command line will run tests in each file matching the pattern
67+
"*_test.exs" found in the `test` directory of your project.
68+
69+
By convention, you could also create a test_helper.exs file inside the
70+
`test` directory and put the code common to all tests there.
71+
72+
The minimum example of a test_helper.exs file would be:
73+
74+
# test/test_helper.exs
75+
ExUnit.start
76+
77+
Then, in each test file, require test_helper.exs before defining test modules
78+
(or cases):
79+
80+
# test/myproject_test.exs
81+
Code.require_file "test_helper.exs", __DIR__
82+
83+
# ... test cases follow
84+
6285
"""
6386

6487
use Application.Behaviour

lib/ex_unit/lib/ex_unit/callbacks.ex

Lines changed: 54 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,71 @@
11
defmodule 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

lib/ex_unit/lib/ex_unit/case.ex

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,18 @@ defmodule ExUnit.Case do
99
in parallel with others. Must be used for performance
1010
when your test cases do not change any global state;
1111
12-
This module automatically includes all callbacks defined
13-
in `ExUnit.Callbacks`. Read it for more information.
12+
This module automatically includes all callbacks defined in
13+
`ExUnit.Callbacks`. See that module's documentation for more
14+
information.
1415
15-
## Examples
16+
## Examples
1617
1718
defmodule AssertionTest do
19+
# Use the module
1820
use ExUnit.Case, async: true
1921
20-
def test_always_pass
22+
# The `test` macro is imported by ExUnit.Case
23+
test "always pass" do
2124
assert true
2225
end
2326
end

0 commit comments

Comments
 (0)