summaryrefslogtreecommitdiffhomepage
path: root/erlang.html.markdown
diff options
context:
space:
mode:
authorven <vendethiel@hotmail.fr>2015-10-03 18:37:31 +0200
committerven <vendethiel@hotmail.fr>2015-10-03 18:37:31 +0200
commit9b46fe7e57bcf63785b67cdaffc49ff0d47d7476 (patch)
treef17e317b6cd951d5f4f1b22b7cb990316781666d /erlang.html.markdown
parent907f4515ca8a80679213d5c0268453fdec3572e1 (diff)
parent61597e603f78dc0645517a13b0b258236e5a3563 (diff)
Merge pull request #1279 from edholland/erlang-eunit
Add section on eunit in erlang doc
Diffstat (limited to 'erlang.html.markdown')
-rw-r--r--erlang.html.markdown33
1 files changed, 33 insertions, 0 deletions
diff --git a/erlang.html.markdown b/erlang.html.markdown
index d0af7f05..64330867 100644
--- a/erlang.html.markdown
+++ b/erlang.html.markdown
@@ -302,6 +302,39 @@ CalculateArea ! {circle, 2}. % 12.56000000000000049738
% The shell is also a process; you can use `self` to get the current pid.
self(). % <0.41.0>
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%% 5. Testing with EUnit
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+% Unit tests can be written using EUnits's test generators and assert macros
+-module(fib).
+ -export([fib/1]).
+ -include_lib("eunit/include/eunit.hrl").
+
+ fib(0) -> 1;
+ fib(1) -> 1;
+ fib(N) when N > 1 -> fib(N-1) + fib(N-2).
+
+ fib_test_() ->
+ [?_assert(fib(0) =:= 1),
+ ?_assert(fib(1) =:= 1),
+ ?_assert(fib(2) =:= 2),
+ ?_assert(fib(3) =:= 3),
+ ?_assert(fib(4) =:= 5),
+ ?_assert(fib(5) =:= 8),
+ ?_assertException(error, function_clause, fib(-1)),
+ ?_assert(fib(31) =:= 2178309)
+ ].
+
+% EUnit will automatically export to a test() fucntion to allo running the tests
+% in the erlang shell
+fib:test()
+
+% The popular erlang build tool Rebar is also compatible with EUnit
+% ```
+% rebar eunit
+% ```
+
```
## References