Property-Based Testing with PropEr, Erlang, and Elixir (Pragprog)

Bought it with the coupon code of course! Now we just need a distributed systems in erlang/elixir book…

3 Likes

You should post that here :003:

1 Like

Is something wrong with Designing for Scalability with Erlang/OTP? It’s published by O’Reilly instead of Prag, but it is an excellent book on the topic.

2 Likes

After I posted I googled a bit and found that book. Added to my list of books to read.

Please don’t forget to review it when you get around to it :003:

Also, @FrancescoC is a member here… I wonder if he’ll be writing any Elixir books any time soon? :101:

1 Like

It should be noted that the eBook is “DRM free” on Google Play (seems to be standard for O’Reilly titles) and ebooks.com.

1 Like

I’m just learning about property based testing and using it in a couple projects. I was wondering how relevant this book is if you are using StreamData instead of PropEr and PropCheck?

I had a look at the excerpts but it is hard to tell.

It is focusing on PropEr and PropCheck, and this book provides explanation for both Erlang and Elixir, but quoting the book…

The concepts should not be hard to carry over from framework to framework in any case.

StreamData is exclusive to Elixir.

1 Like

The book is divided into 3 parts:

  • the basics
  • stateless properties in practice
  • stateful properties

Parts 1 should be useful regardless of the tool used, as it covers more foundational knowledge (thinking in properties, etc.). Part 2 should still be useful if using StreamData, but you’ll obviously have to adapt the code. Part 3 cannot be used with StreamData, as StreamData doesn’t provide state checking (StreamData: data generation and property testing for Elixir)

You can probably get a good grasp of how helpful the book would be to you by reading http://propertesting.com which seems to cover most of part 1 in the book (it’s by the same author), although code samples are only in Erlang.

Hope this helps!

1 Like

As others have said, you should be able to apply a lot of the thinking with stream data although some of the operations just aren’t supported right now so I’d suggest going through the book using propcheck. We use both at work. StreamData for very basic generation of data and Proper for when we need more robust testing, model checking, etc.

Thanks everyone. This is just the information I was looking for. :slight_smile:

https://twitter.com/mononcqc/status/1058002299480629250?s=21

3 Likes

Hi everyone,

I just wanted to announce that Property-Based Testing with PropEr, Erlang, and Elixir is now finally out!

proper

It is probably the friendliest introduction you can get to Property-Based Testing in this community, and also one of the best resource to bring you to a reasonably advanced level of skill.

If you’re interested in more details, please see any of:

If you have previously read any of the beta copies, know that the final version now has both Erlang and Elixir code inline, at the same time (rather than Elixir in an appendix as in earlier beta copies). It was quite the fun challenge to make one book work for two languages, but I think you’ll find it works very well. If you only know either language, chances are you’ll feel comfortable reading both anyway before you’re halfway through.

And here’s also a twitter link in case any of you feels like making some noise on my behalf :slight_smile: https://twitter.com/mononcqc/status/1083027123777036291

Thanks, and enjoy the read!

10 Likes

Congratulations Fred!

Thanks for all your hard work - not just on this book but all of them - I look forward to reading them all!

I have also liked and retweeted your tweet :023:

3 Likes

THANK YOU for the discount!!!

I just wrote an article about this book on Erlang Battleground - A Blog

1 Like

The Philly Elixir Group will be using Property-Based Testing with PropEr, Erlang, and Elixir for their next online book club, starting Jan 28th. If anyone outside Philly would like to join us, there are very welcome!

2 Likes

Thank you so much! I bought it on PragProg.

I’m still struggling to see the counter-examples with Elixir, though. I tried

File.read!("_build/propcheck.ctex")
|> :erlang.binary_to_term()

and got an ArgumentError.

UPDATE

I found that the couter-examples file _build/propcheck.ctex is a DETS table, whose keys are mfargs, but I’m confusing about the values. They seem to always be [[]].

I’m not 100% sure of it but looking at https://github.com/alfert/propcheck/blob/bcdb467546c5853ef7d529bd987f2638bb23ddbc/lib/counterstrike.ex, it appears that the format is {mfa, counterexamples}. I would expect mfa to be the code behind the failing property, and counterexamples a list of the specific inputs that can make it fail.

So the counterexamples being [[]] means that there is a single counterexample when the input is []. These are passed directly to PropEr when re-running the property: https://github.com/alfert/propcheck/blob/3e81043dd090085f1bfd5bb9f70d44acc5294e1c/lib/properties.ex#L177 – the library then does the rest.

The way I handled them in the rebar3 plugin is a bit different, but I rely on the same sort of functionality. The PropEr library hands the counterexample back to you upon failure, and if you send the same arguments back you should get the same failure out if nothing changed.

1 Like

Thanks. It seems my toy property fails when the input is an empty list, and Elixir treats [[]] as an empty iodata so prints an empty line.

Now I’m facing another problem: PropCheck seems not doing shrinking. I’m following the book and implemented the biggest(list) function as

def biggest([head|tail]), do: biggest(tail, head)

defp biggest([], max), do: max
defp biggest([head|tail], max) when head > max, do: biggest(tail, head)
defp biggest([head|tail], max) when head < max, do: biggest(tail, max)

The minimal input that can cause a failure is a list with 2 identical elements, but when I run

PROPCHECK_VERBOSE=1 mix test

PropCheck outputs

Failed: After 24 test(s).
An exception was raised:
** (FunctionClauseError) no function clause matching in Pbt.BiggestTest.biggest/2
Stacktrace:


[6, 6, 1, -3, -5, -3, -3]

Shrinking (0 time(s))
[6, 6, 1, -3, -5, -3, -3]
.................................................................

  1) property finds biggest element (Pbt.BiggestTest)
     test/biggest_test.exs:5
     Property Elixir.Pbt.BiggestTest.property finds biggest element() failed. Counter-Example is:
     [[6, 6, 1, -3, -5, -3, -3]]
     
     Counter example stored.
     
     code: nil
     stacktrace:
       (propcheck 1.2.2) lib/properties.ex:227: PropCheck.Properties.handle_check_results/2
       test/biggest_test.exs:5: (test)

Interestingly, when I code the property in Erlang, PropEr is doing shrinking correctly.