eunit进行单元測試时的方法
1、測試代碼写入头文件件中.
主程序中加入
-ifdef(TEST).?
-include("module_tests.hrl").?
-endif.?
在module_tests.hrl中写入
-include_lib("eunit/include/eunit.hrl").?
module_test_() ->
?? [?assertEqual(ok, public_function(),
?
?? ??assertEqual(error, private_function())].
2、-compile(export_all)
-ifdef(TEST).
-compile(export_all).
-endif.
然后另写測試Module,进行測試.
?
==============================================
这样写的好处:
产品代碼和測試代碼分开.
产品代碼编译时,可不编译測試代碼.
私有的方法可以进行測試.
单元測試易修改.
?
注:TEST测定可在erlide下,Erlang Compiler中输入{d, 'TEST'}.