足し算関数をeunitでテスト
eunitは実行するとモジュール内のtest関数を探して試験してくれます
main.erl
-module(main).
-author("haminiku").
%% API
-export([add/2]).
add(A, B) -> A + B.
tests.erl
-module(tests).
-include_lib("eunit/include/eunit.hrl").
-author("haminiku").
%% API
-export([test/0]).
%% テスト実行
%% erl -noshell -pa ebin -eval "eunit:test(test_module, [verbose])" -s init stop
test() ->
add_test(),
assert_test().
add_test() ->
4 = main:add(2, 2),
5 = main:add(2, 2).
assert_test() ->
?assertEqual(4, main:add(2, 2)),
?assertEqual(5, main:add(2, 2)).
%% 関数名にtestが無いので実行されない
sample() -> ?assertEqual(1000, main:add(2, 2)).
# コンパイル
>>>erlc ./tests.erl
# テスト実行
>>>erl -noshell -pa ebin -eval "eunit:test(tests, [verbose])" -s init stop
======================== EUnit ========================
module 'tests'
tests: add_test...*failed*
in function tests:add_test/0 (tests.erl, line 17)
**error:{badmatch,4}
output:<<"">>
tests: assert_test...*failed*
in function tests:'-assert_test/0-fun-1-'/1 (tests.erl, line 21)
**error:{assertEqual,[{module,tests},
{line,21},
{expression,"main : add ( 2 , 2 )"},
{expected,5},
{value,4}]}
output:<<"">>
[done in 0.009 s]
=======================================================
Failed: 2. Skipped: 0. Passed: 0.