正如 Pascal 所写,不幸的是 EUnit 没有测试用例自动注册。
另一方面,如果你内联编写测试函数并使用“测试标题”,你可以获得自动注册的等价物:
fixture_test_() ->
{
setup,
fun test_setup/0,
fun test_teardown/1,
[
% Simplest possible. One single assertion.
% Note the presence of the "_" prefix.
{"description of test case 1",
?_assert(true)
},
% Slighly more complicated: multiple assertions inside a list.
% Note the presence of the "_" prefix.
{"description of test case 2", [
?_assert(true),
?_assert(true),
?_assert(true)
]},
% Even more complicated, when you really need a function.
% Note the absence of the "_" prefix.
{"description of test case 3",
fun() ->
X = f1(),
Y = f2(X),
?assertEqual(foo, Y)
end}
]
}.
在详细模式下运行时:
$ rebar eunit suite=pizza
==> mcs (eunit)
======================== EUnit ========================
module 'pizza'
module 'pizza_tests'
pizza_tests:29: fixture_test_ (description of test case 1)...ok
description of test case 2
pizza_tests:34: fixture_test_...ok
pizza_tests:35: fixture_test_...ok
pizza_tests:36: fixture_test_...ok
[done in 0.009 s]
pizza_tests: fixture_test_ (description of test case 3)...ok
[done in 0.015 s]
[done in 0.015 s]
=======================================================
All 5 tests passed.