EUnit is perfectly suitable to test multiple processes and DETS, don't worry.
I think that the only case of a test failing and as a result not closing the DETS file, it is because you are not using a fixture.
That is, a code like this:
wrong_test() ->
setup(),
?assert(false),
cleanup().
will NOT call cleanup(), because the line with the ?assert()
will throw an exception. This is expected behavior. So if the cleanup() is supposed to close the DETS file, it will not close it.
The EUnit documentation explains that the way to be sure that the cleanup function is always executed, no matter what happens to the tests, is to use a "fixture", either setup
or foreach
. For example:
correct_test_() ->
{setup,
% Setup
fun() ->
?assertMatch({ok, Table}, dets:open_file("hello", [])),
Table
end,
% Cleanup
fun(Table) ->
?assertMatch(ok, dets:close(Table)).
end,
% Tests
[
% This will fail, but the cleanup WILL be called
?_assert(false)
]
}.
So, there is no need to "catch" an exception in Erlang for this particular case. You obtain the same using a fixture.
Regarding the fact that you couldn't close the DETS file from the shell, this will not happen with a fixture. In addition, also with your buggy test, it is not a problem, because the file will be closed properly when exiting from the Erlang shell. The only time when a DETS file is not closed properly is when the Erlang runtime system itself crashes.
Other helpful source of documentation, easier to understand than the very terse official one I mentioned before, are the LYSE chapter on Eunit and two presentations about Eunit that you can find on the Erlang Factory web site.