Thinking in Properties

Question 1

Your answer could be any three of these strategies:

  • Modeling—comparing the implementation with a simpler but obviously correct one
  • Generalizing traditional tests by automating steps we would do by hand
  • Finding program invariants to validate
  • Using symmetric properties

Question.

Question 2

Two example solutions:

 Erlang code/ThinkingInProperties/erlang/pbt/test/prop_solutions.erl
 %% @doc this function tests that any lists of `{Key,Val}' pairs
 %% end up being able to be sorted by the key by using `lists:keysort/2'.
 prop_keysort1​() ->
  ?FORALL(List, ​list​({​term​(),​term​()}),
 begin
 %% is_key_ordered checks that all tuples' keys are ordered.
 is_key_ordered​(​lists​:​keysort​(1, List))
 end​).

Get Property-Based Testing with PropEr, Erlang, and Elixir now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.