Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Property-based Testing is a Mindset

Property-based Testing is a Mindset

Andrea Leopardi

December 06, 2017
Tweet

More Decks by Andrea Leopardi

Other Decks in Programming

Transcript

  1. I S A M I N D S E T

    P R O P E R T Y - B A S E D T E S T I N G
  2. test "sorting" do assert sort([]) == [] assert sort([1, 2,

    3]) == [1, 2, 3] assert sort([2, 1, 3]) == [1, 2, 3] end example-based
  3. test "sorting" do assert sort([]) == [] assert sort([1, 2,

    3]) == [1, 2, 3] assert sort([2, 1, 3]) == [1, 2, 3] end
  4. check all list <- list_of(int()) do sorted = sort(list) assert

    is_list(sorted) assert same_elements?(list, sorted) assert ordered?(sorted) end
  5. check all list <- list_of(int()) do sorted = sort(list) assert

    is_list(sorted) assert same_elements?(list, sorted) assert ordered?(sorted) end
  6. check all list <- list_of(int()) do sorted = sort(list) assert

    is_list(sorted) assert same_elements?(list, sorted) assert ordered?(sorted) end
  7. check all list <- list_of(int()) do sorted = sort(list) assert

    is_list(sorted) assert same_elements?(list, sorted) assert ordered?(sorted) end
  8. check all list <- list_of(int()) do sorted = sort(list) assert

    is_list(sorted) assert same_elements?(list, sorted) assert ordered?(sorted) end
  9. property "encoding->decoding is circular" do check all bin <- binary()

    do encoded = Huffman.encode(bin) assert is_binary(encoded) assert Huffman.decode(encoded) == bin end end Huffman encoding
  10. property "gives same results as Erlang impl" do check all

    bin <- binary() do assert Huffman.encode(bin) == :huffman.encode(bin) end end
  11. https://www.youtube.com/watch?v=jvwfDdgg93E property "only expected codes are returned" do check all

    request <- request() do response = HTTP.perform(request) assert response.status in [200, 201, 400, 404] end end
  12. R E A L - W O R L D

    E X A M P L E
  13. property "splitting commands at random" do check all cmd <-

    command(), split_cmd <- random_splits(cmd) do assert {:ok, _, ""} = parse_many(split_cmd) end end
  14. property "splitting commands at random" do check all cmd <-

    command(), split_cmd <- random_splits(cmd) do assert {:ok, _, ""} = parse_many(split_cmd) end end
  15. property "splitting commands at random" do check all cmd <-

    command(), split_cmd <- random_splits(cmd) do assert {:ok, _, ""} = parse_many(split_cmd) end end