Help us understand the problem. What is going on with this article?

Erlang - EUnitで単体テスト

More than 5 years have passed since last update.

足し算関数を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.
haminiku
やや,かみごたえがある。T字形の骨をはさんで片側にヒレ,片側にサーロインの肉のついた大きな塊
http://subc.github.io/
dena_coltd
    Delight and Impact the World
https://dena.com/jp/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away