[erlang-questions] Structuring an Eunit Suite

Richard Carlsson richardc@REDACTED
Sun Jan 25 14:41:45 CET 2009


Ben Hood wrote:
> I have a suite of tests that each connect to a message server and
> execute a number of commands.
> 
> I want to be able to parameterize where the connection supplied from,
> so that I can re-use the tests on different transports.
> 
> What I've got now is a suite that looks like this:
> 
> foo_test() ->
>      test_util:foo_test(new_connection()).
> ......
> 
> new_connection() ->
>      %% returns a specific connection to the server
> 
> ......
> 
> and all of the real tests are in the test_util module, e.g:
> 
> foo_test(Connection) ->
>      %% assert something
> 
> Ideally I would like eunit to do something like
> 
> "foreach _test function in the test_util module, create a new
> connection and pass it as a function argument"

Sorry, but I forgot to reply to this question.
Here's a sketch for doing this sort of thing:

foo_test_() ->
  {setup,
   fun new_connection/0,
   fun close_connection/1,
   {with,
    [fun test_util:foo_test/1,
     fun test_util:bar_test/1,
     fun test_util:baz_test/1
    ]}
  }.

The drawback being that you need to manually list the "abstract test
functions" (tests that need an argument) within the {with, List} tuple.

I should perhaps add some sort of utility function to eunit for
enumerating functions in general, based on regexps...

    /Richard



More information about the erlang-questions mailing list