[erlang-questions] Question about foreach in Eunit

Josh Kuhn <>
Thu Nov 20 19:13:16 CET 2008


Hi,

I'm new to eunit and I am trying to get a certain group of tests completed
that require a setup and teardown. Specifically, I have an ets table that I
would like to set up and then drop after each small test. The problem is,
the eunit macro for this seems to be only performing the setup and teardown
at the beginning of the series and at the end. I am using a "foreach", but
it is acting like a "setup".  I have some code below to illustrate what I am
talking about:

db_functions_test_() ->
    {foreach,
     fun()->
             {ets:new(usertable,[set,public]),
              ets:new(addrtable,[set,public])}
     end,
     fun({Users,Addrs})->
             ets:delete(Users),
             ets:delete(Addrs)
     end,
     [fun db_test_list_/1]
    }.

db_test_list_(Tables) ->
    [{"Add user to empty table",

?_assertMatch(user_added,fastrets:add_user(Tables,"Test",{'localhost',4000}))},
     {"Get all users from empty table",
      ?_assertMatch([],fastrets:get_all_users(Tables))},
<*Lots more tests here -snip-*>

I am using this particular form since I need the ets table info for the
functions in the fastrests module, so I need to be able to initialize each
of the tests with the newly generated data. In this case, the behavior I am
getting is that the second test is failing to find an empty list because the
first test added a user and wasn't cleaned up.   I understand that ets is
not garbage collected, so could it be that it is simply not possible to do
these tests in the manner I am attempting?  Also, I am fairly new to unit
testing in general (I am a student) and eunit in particular, so if there is
a more idiomatic or reasonable way to do what I am trying to do, I am all
ears.

Thanks,

Josh Kuhn
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20081120/994b9624/attachment.html>


More information about the erlang-questions mailing list