Eunit addons make it easier to work with tests that require some kind of setup/cleanup to be performed before/after each test.
Include the following in your eunit test suite and you should be on your way. You don't have to include the eunit/include/eunit.hrl, since that's automatically included by the line below:-include_lib("eunit_addons/include/eunit_addons.hrl").
This adds a set of macros which help you write eunit tests.
NOTE: Use one of the ?WITH_SETUP macros with fewer parameters (if
possible), since these don't require that the list of tests
to be run are be specified but rather deduce that from the
test module itself. However, the version of the macro which
has the Tests
parameter might be useful when there are
different groups of tests in one module which require
different setup.
The {spawn,Test}
feature of eunit is used to achieve test isoloation.
?WITH_SETUP(SetupFun, CleanupFun)is equivalent to:
?WITH_SETUP(SetupFun, CleanupFun, 120, 30)
See ?WITH_SETUP/4 for details.
Example: Run all functions named..._test/1
in the current module
with default timeouts. Start a server as part of the setup.
The ..._test_/0
function is a test generator (this is how eunit
works; it must be named ..._test_/0
). The ?WITH_SETUP macro will
only look for functions named ..._test/1
and run these.
my_server_test_() -> ?WITH_SETUP(fun() -> {ok, Pid} = my_server:start_link(), Pid end, fun(Pid) -> my_server:stop(Pid) end). returns_foo_test(Pid) -> foo = my_server:get_foo(Pid). returns_bar_test(Pid) -> bar = my_server:get_bar(Pid).In the above example, functions are called in this order:
setup fun() -> Pid1 returns_foo_test(Pid1) cleanup fun(Pid1) setup fun() -> Pid2 returns_bar_test(Pid2) cleanup fun(Pid2)
?WITH_SETUP(SetupFun, CleanupFun, ForAllTimeout, PerTcTimeout)is equivalent to:
?WITH_SETUP(SetupFun, CleanupFun, ForAllTimeout, PerTcTimeout, ListOfAllTestFunctionsInModuleWithArityOne)
See ?WITH_SETUP/5 for details.
Example: Run all functions named..._test/1
in the current module
with user-defined timeouts. Start a server as part of the setup.
The ..._test_/0
function is a test generator (this is how eunit
works; it must be named ..._test_/0
). This ?WITH_SETUP macro will
run all tests in Tests
.
my_server_test_() -> ?WITH_SETUP(fun() -> {ok, Pid} = my_server:start_link(), Pid end, fun(Pid) -> my_server:stop(Pid) end, 120, % timeout for all tests 60). % timeout for each test returns_foo_test(Pid) -> foo = my_server:get_foo(Pid). returns_bar_test(Pid) -> bar = my_server:get_bar(Pid).In the above example, functions are called in this order:
setup fun() -> Pid1 returns_foo_test(Pid1) cleanup fun(Pid1) setup fun() -> Pid2 returns_bar_test(Pid2) cleanup fun(Pid2)
?WITH_SETUP(SetupFun, CleanupFun, ForAllTimeout, PerTcTimeout, Tests) SetupFun = () -> State CleanupFun = (State) -> void() State = term() ForAllTimeout = integer() PerTcTimeout = integer() Tests = [Test] Test = atom()
Runs SetupFun
before and CleanupFun
after each Test
.
SetupFun
is a zero-argument fun which may do any setup that has
to be done before the test is performed. The result of this fun,
State
, is passed to each of the Tests
as well as the
CleanupFun
. The CleanupFun
is supposed to take care of any
cleanup after the test and is called regardless of whether the
test was successful or not. Each Test
must be a function of
arity 1 (the parameter is the output from the SetupFun
).
ForAllTimeout
is the maximum time (in seconds) all the Tests
are allowed to take. PerTcTimeout
is the maximum time (in
seconds) a single Test
is allowed to take.
Tests
is a list of names of functions (arity 1). Each of these
Test
functions will be passed the output from the SetupFun
.
my_server_foo_test_() -> ?WITH_SETUP(fun setup_before_foo/0, fun cleanup_after_foo/1, 120, % timeout for all tests 60, % timeout for each test [returns_foo_test, % run only `foo' tests sets_foo_test]). returns_foo_test(Pid) -> foo = my_server:get_foo(Pid). sets_foo_test(Pid) -> my_server:set_foo(Pid, foo2). foo2 = my_server:get_foo(Pid). my_server_bar_test_() -> ?WITH_SETUP(fun setup_before_bar/0, fun cleanup_after_bar/1, 120, % timeout for all tests 60, % timeout for each test [returns_bar_test]). % run only `bar' tests returns_bar_test(Pid) -> bar = my_server:get_bar(Pid).In the above example, functions are called in this order:
%% foo tests setup_before_foo() -> Pid1 returns_foo_test(Pid1) cleanup_after_foo(Pid1) setup_before_foo() -> Pid2 sets_foo_test(Pid2) cleanup_after_foo(Pid2) %% bar tests setup_before_foo() -> Pid3 returns_bar_test(Pid3) cleanup_after_bar(Pid3)
get_tests_with_setup/1 | Return a list of tests which require a setup (..._test/1). |
get_tests_with_setup(Module) -> [{Fn, Arity}]
Return a list of tests which require a setup (..._test/1).
Generated by EDoc, Sep 27 2016, 22:14:45.