1

I have been playing with EUnit, it is nice but I'm running into issue with dets, when my test failed and haven't properly closed dets, the file is still open in my shell and I cannot close it because it was created by another process(when i ran tests).

Have you ran into the same issue? Can I do try catch in EUnit efficiently ?

Thanks for any input!

Martin Bodocky
  • 651
  • 1
  • 5
  • 16

2 Answers2

0

Use Common Test. EUnit suitable for testing small function without side effects.

saa
  • 245
  • 5
  • 10
  • Although Common Test is perfectly viable, this statement about EUnit is completely wrong. EUnit can perfectly test what the OP is asking about, and a lot more. – marco.m Jan 17 '16 at 16:46
0

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.

marco.m
  • 4,573
  • 2
  • 26
  • 41