you're reading...
Erlang, HowTo

Erlang Quickcheck Design Patterns – Black Box

Black Box

Also Known As
ETS Driven Testing

When writing Quickcheck unit tests for an Erlang application, it usually happens that you require to stub some external application, since you’re not interested in System Tests, but you want to test your application as a mere black box.

You want complete control on the results provided by your stubs, so that you can successfully check the properties you need for your application.

Sample Code

In your quickcheck module, just define in your state the results you need, together with their default value:

-record(state, {stub_result = true}).

Define the initial state as follows:

initial_state() ->

initial_state_data() ->

In your property, create an ETS table containing the results (don’t forget to delete it after running the tests) and call the run_commands function by passing an extra parameter:

prop_commands() ->
    ?FORALL({Cmds, State}, custom_commands(),
                          results = ets:new(results, [named_table, set, public]),
                          ets:insert(results, {stub_result, State#state.stub_result}),
                          {H,S,Res} = eqc_fsm:run_commands(?MODULE, Cmds, [{var, stub_result}]),
                          true = ets:delete(results),

The custom_commands function is the place where you set the list of available results:

custom_commands() ->
         #state{stub_result = oneof([true, false])},
         {more_commands(100, eqc_fsm:commands(?MODULE, {init, State})), State}).

You can check the result in your postconditions, as follows:

postcondition(_From, _To, S, {call, _, _, _}, R) ->
    S#state.stub_result == R.

Obviously, your stubs need to contain something like:

stubbed_function() ->
    ets:lookup_element(results, stub_result, 2).

Your tests will be able to cover a bigger number of code lines (lemmings).

Thanks to Master Magnus Henoch for his help.

About Roberto Aloi

Software Engineer and Erlanger. Author of www.tryerlang.org.


No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: