[erlang-questions] PropEr after test clean up

Kostis Sagonas kostis@REDACTED
Tue May 26 01:44:16 CEST 2015


On 05/25/2015 05:11 PM, Hynek Vychodil wrote:
>
> I bumped in the problem how clean up after property in PropEr. Let's
> have simple property where I make ets table:
>
> prop_ets() ->
>      ?FORALL(V, integer(),
>          begin
>              E = ets:new(),
>              true = ets:insert(T, {V, ok}),
>              equals([{V, ok}], ets:lookup(T, V))
>          end
>      ).
>
> How am I supposed to delete ets table? It is trickier than looks like.
> ... <SNIP>

I am not sure I understand what it is exactly that you are finding 
difficult to do in the simple example above.  Here is how:

=====================================================================
-module(pets).
-include_lib("proper/include/proper.hrl").

-export_type([key/0, val/0]). % shut off compiler warning

-type key() :: a | b | c.
-type val() :: 0..42.

prop_ets() ->
   ?FORALL({K,V}, {key(),val()},
	  begin
	      T = setup(),
	      true = ets:insert(T, {K, V}),
	      Res = ets:lookup(T, K),
	      cleanup(T),
	      [{K, V}] =:= Res
	  end).

setup() ->
    ets:new(?MODULE, [public, named_table]).

cleanup(T) ->
    ets:delete(T).
========================================================================

The pattern of doing a setup, running some commands and getting some 
Result, doing the cleanup you want to do, and the check that the result 
is the one that you expect is a general pattern in property-based testing.

As you can see, the above works:

kostis@REDACTED:~/proper$ erl -pa ~/proper
Erlang/OTP 17 [erts-6.4.1] [source] [64-bit] [smp:8:8] 
[async-threads:10] [hipe] [kernel-poll:false]

Eshell V6.4.1  (abort with ^G)
1> c(pets, [debug_info]).
{ok,pets}
2> proper:quickcheck(pets:prop_ets()).
....................................................................................................
OK: Passed 100 test(s).
true


Change the property to one that does not hold (e.g. [{K, V}] =/= Res) 
and you will also see that shrinking works.


I am not sure I follow what you mean by your nested ?FORALL properties 
but note that, as shown above, you can easily imitate multiple ?FORALLs 
by using complex terms (tuples, lists, etc.).

Hope this helps,
Kostis



More information about the erlang-questions mailing list