Skip to content

Commit 985dae5

Browse files
Updates to test execution documentation (#940)
Co-authored-by: Angelika Tyborska <[email protected]> Co-authored-by: czrpb <[email protected]>
1 parent 29ee0f4 commit 985dae5

File tree

2 files changed

+121
-4
lines changed

2 files changed

+121
-4
lines changed

docs/TESTS.md

Lines changed: 103 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Running tests
22

3-
From the terminal, change to the base directory of the problem then execute the tests with:
3+
From the terminal, change to the base directory of the exercise then execute the tests with:
44

55
```bash
66
$ mix test
@@ -10,7 +10,7 @@ This will execute the test file found in the `test` subfolder -- a file ending i
1010

1111
## Pending tests
1212

13-
In the test suites, all but the first test have been skipped.
13+
In test suites of practice exercises, all but the first test have been tagged to be skipped.
1414

1515
Once you get a test passing, you can unskip the next one by
1616
commenting out the relevant `@tag :pending` with a `#` symbol.
@@ -24,13 +24,114 @@ test "shouting" do
2424
end
2525
```
2626

27+
If you wish to run all tests at once, you can include all skipped test by using the `--include` flag on the `mix test` command:
28+
29+
```bash
30+
$ mix test --include pending
31+
```
32+
2733
Or, you can enable all the tests by commenting out the
2834
`ExUnit.configure` line in the test suite.
2935

3036
```elixir
3137
# ExUnit.configure exclude: :pending, trace: true
3238
```
3339

40+
## Additional Elixir testing features
41+
42+
`ExUnit` and `mix test` provide quite a few methods of grouping, tagging & executing
43+
tests and various methods of controlling test execution, much of which is summarized
44+
below.
45+
46+
### Methods for running specific tests
47+
48+
Documentation:
49+
50+
* [`mix test` documentation](https://hexdocs.pm/mix/Mix.Tasks.Test.html)
51+
52+
#### Running tests in a specific file
53+
54+
All tests in a single file may be executed via `mix test` by specifying the file:
55+
56+
```bash
57+
$ mix test test/<FILE>.exs
58+
```
59+
60+
> NOTE: `tagging` may impact which tests are actually executed using this method.
61+
62+
#### Running individual tests
63+
64+
Individual tests may be executed by referencing a test's line number in the file:
65+
66+
```bash
67+
$ mix test test/<FILE>.exs:LINENUM
68+
```
69+
70+
Multiple tests may be executed by giving multiple line numbers separated by `:`.
71+
72+
For example, given a file with the following content with line numbers:
73+
74+
```elixir
75+
test "Test 1" do # 1
76+
# test implementation # 2-6
77+
end # 7
78+
# 8
79+
test "Test 2" do # 9
80+
# test implementation # 10-21
81+
end # 22
82+
# 23
83+
test "Test 3" do # 24
84+
# test implementation # 25-35
85+
end # 36
86+
```
87+
88+
The 1st and 3rd tests can be executed by:
89+
90+
```bash
91+
$ mix test test/FILE.exs:1:24
92+
```
93+
94+
> NOTE: When specifying tests via line numbers, `tagging` is ignored.
95+
96+
#### Running groups of tests
97+
98+
Tests may be grouped using `describe`:
99+
100+
```elixir
101+
describe "short test group description" do
102+
test "test description" do
103+
# test implementation
104+
end
105+
106+
test "another test description" do
107+
# test implementation
108+
end
109+
end
110+
```
111+
112+
All tests in a group may be executed by referencing its line number in the file,
113+
just like referencing and executing individual tests.
114+
115+
Documentation:
116+
117+
* [`describe`](https://hexdocs.pm/ex_unit/ExUnit.Case.html#describe/2)
118+
119+
#### Other useful `mix test` options
120+
121+
* `--include` and `--exclude` - runs or doesn't run specific tests based on their `@tag`s
122+
* `--failed` - runs only tests that failed the last time they ran
123+
* `--max-failures` - the suite stops evaluating tests when this number of test failures
124+
is reached
125+
* `--seed` - seeds the random number generator used to randomize the order of tests
126+
`--seed 0` disables randomization so the tests in a single file will always be ran
127+
in the same order they were defined in
128+
* `--stale` - runs only tests which reference modules that changed since the last
129+
time tests were ran with `--stale`
130+
131+
Documentation:
132+
133+
* [`mix test` command-line options](https://hexdocs.pm/mix/Mix.Tasks.Test.html#module-command-line-options)
134+
34135
## Typespecs and Dialyzer (DIscrepancy AnalYZer for ERlang programs)
35136

36137
Elixir exercises include a skeleton implementation file in the `lib`

exercises/shared/.docs/tests.md

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
# Running tests
22

3-
Execute the tests with:
3+
From the terminal, change to the base directory of the exercise then execute the tests with:
44

55
```bash
66
$ mix test
77
```
88

9+
This will execute the test file found in the `test` subfolder -- a file ending in `_test.exs`
10+
11+
Documentation:
12+
13+
* [`mix test` - Elixir's test execution tool](https://hexdocs.pm/mix/Mix.Tasks.Test.html)
14+
* [`ExUnit` - Elixir's unit test library](https://hexdocs.pm/ex_unit/ExUnit.html)
15+
916
## Pending tests
1017

11-
In the test suites, all but the first test have been skipped.
18+
In test suites of practice exercises, all but the first test have been tagged to be skipped.
1219

1320
Once you get a test passing, you can unskip the next one by commenting out the relevant `@tag :pending` with a `#` symbol.
1421

@@ -32,3 +39,12 @@ Or, you can enable all the tests by commenting out the `ExUnit.configure` line i
3239
```elixir
3340
# ExUnit.configure(exclude: :pending, trace: true)
3441
```
42+
43+
## Useful `mix test` options
44+
45+
* `test/<FILE>.exs:LINENUM` - runs only a single test, the test from `<FILE>.exs` whose definition is on line `LINENUM`
46+
* `--failed` - runs only tests that failed the last time they ran
47+
* `--max-failures` - the suite stops evaluating tests when this number of test failures
48+
is reached
49+
`--seed 0` - disables randomization so the tests in a single file will always be ran
50+
in the same order they were defined in

0 commit comments

Comments
 (0)