[erlang-questions] Structuring an Eunit Suite
Adam Lindberg
adam@REDACTED
Thu Jan 15 12:23:04 CET 2009
Hi Ben,
Let's see if I understood you correctly...
The example in the EUnit documentation:
fib_test_() ->
[?_assert(fib(0) == 1),
?_assert(fib(1) == 1),
?_assert(fib(2) == 2),
?_assert(fib(3) == 3),
?_assert(fib(4) == 5),
?_assert(fib(5) == 8),
?_assertException(error, function_clause, fib(-1)),
?_assert(fib(31) == 2178309)
].
Could be written as (without the exception assert):
fib_test_() ->
lists:map(fun assert_fib/1, [{0,1}, {1,1}, {2,2}, {3,3}, {4,5}, {5,8}, {31,2178309}]).
assert_fib({Value, Expected) ->
?_assert(fib(Value) == Expected).
If foo_test(Connection) returns a test fun, e.g. ?_assert(...) or similar, and you want to run this test for many different types of connections you could generate a list of such tests:
foo_test_() -> % Note the trailing underscore
[test_util:foo_test(new_connection()), test_util:foo_test(some_other_connection())].
Note that the function name ends in underscore. This means that the function is not a test itself, it returns a test (which will be executed at a later time). Even nicer could be like this:
foo_test_() ->
lists:map(fun test_util:foo_test/1, [new_connection(), some_other_connection()]).
Does this seem to be what you want?
Cheers,
Adam
----- "Ben Hood" <0x6e6562@REDACTED> wrote:
> Hi,
>
> I'm currently using Eunit in a very agricultural fashion and would
> like to use more of the Eunit infrastructure to make the tests less
> verbose.
>
> 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"
>
> Is this possible?
>
> Thanks for any suggestions,
>
> Ben
>
> PS The link the source is here:
>
> http://hg.rabbitmq.com/rabbitmq-erlang-client/file/437d0e4d66c8/src/network_client_test.erl
> http://hg.rabbitmq.com/rabbitmq-erlang-client/file/437d0e4d66c8/src/test_util.erl
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://www.erlang.org/mailman/listinfo/erlang-questions
More information about the erlang-questions
mailing list