My bad opinions

2019/01/09

Property-Based Testing with PropEr, Erlang, and Elixir

book cover

Today, I'm publishing a new book: Property-Based Testing with PropEr, Erlang, and Elixir. It's my third book, the second one that is not self-published.

Back in 2016, I wrote a blog post titled Property-Based Testing Basics, where I intended to start a short series of introductory blog posts explaining property-based testing.

However, as soon as I started mapping out the content I wanted to cover, I found out there would be a lot of it needed, and it would need to be very framework-specific, since a lot of property-based testing is about automating away a lot of boring stuff to get solid automated tests. In short, blog posts were not the ideal medium for this.

This explains why the book is out today. For maybe over a year and a half (part time) my free time has been spent on the book: one year on the first draft, and maybe one year and a half on the adapting and editing up to PragProg's standards, along with the various reviews.

Since the books' introduction is available online, here's a quote explaining why I decided to write it:

When I finished my first book, Learn You Some Erlang, I told myself "never again". There's something distressing about spending months and years of work writing a book, spending all bits of free times you can find on it, putting aside other projects and hobbies, and rewriting your own texts close to a dozen times. You reach the point where before you're even done, you're tired of writing about the topic you chose to write about.

I knew all of that was waiting for me if I ever wanted to write another book. I decided to do it anyway because I truly believe property-based testing is something amazing, worth learning and using. In fact, part of the reason why I wanted to write a book was that I wanted to use property-based testing in projects at work and online, and it's generally a bad idea to introduce a technology when only one person in the team knows how it works.

It is a better compromise to spend all that time and effort writing a book than never using Property-Based testing in a team when I know what it can do and bring to a project. Hopefully, you'll feel that learning about it in here is worth your time as well.

And because I feel that making the content accessible to everyone is important, one of the aspects of publishing the book I did not want to negotiate one was to be able to keep a free copy (as in beer) available online; the copy though is the book prior to being edited. It contains raw content, non-edited, not reviewed, and with missing stuff.

One of the critical parts of going through editing is that all the hard-to-understand stuff, inconsistent terminology, unclear explanations, and plain confusing content is pointed out and fixed. The text is often re-structured and shuffled around to be more logical. Through the book writing process at PragProg, I've also added Elixir translations for all of the code, and a chapter (Erlang only due to Elixir tooling limitations at the time of writing) on Targeted Properties.

If you want to see the old site, you can head to propertesting.com, although I do recommend the book itself.

So what will you learn if you get a copy of the book? Mostly, we start from the ground up assuming you've got some base knowledge of Erlang or Elixir. Your testing experience should not matter too much, as long as you've done some unit testing before, since I've found it easier to compare how regular tests are done to properties to better highlight their differences. TDD or not won't be a critical, and you don't need to be amazing at testing either.

So we build from the conceptual level: what exactly should a property do? How do we write one? Once you got all the syntax and parts in place, the challenging parts are to think in properties and to know how to generate good data sets. With these, you'll be able to write good unit test properties.

In the second section of the book, we put these in practice with toy projects: one to see when to use traditional unit tests and when to use properties best, and one to see how we could apply Test-Driven Development (TDD) approaches to property-testing while discovering project requirements. The rest of the section is about helping debug your properties and generators through shrinking, and some really exciting stuff called targeted property-based testing. The latter is fully unique to the PropEr framework and shows incredible promise.

The last section of the book covers stateful testing with a model-based approach. This sounds fancy, and it kind of is! It's all about making an abstract, high-level description of what your system should do, and then compare sequences of operations against your system with what they should be doing according to the model. This lets you write terse but amazingly powerful integration and system tests with the same framework you use for unit tests.

So in short, the book should bring you from knowing nothing about property testing to being able to test units and multiple components on a smooth curve. I try to avoid any highly complex descriptions that are often associated with Property-Based Testing (aside from what you've got with Hypothesis which is objectively friendly), and the text should be one of the friendliest resources out there to get in on property testing.

I think Property-Based Testing is worth learning enough to keep the draft available for free (I actually pay to keep it up, it's all ad-free), and I would encourage anyone interested to dive in. Of course, I also believe that the commercial version of the book is much better after having gone through editing and review processes, so if you can get a copy, it's likely to be a nicer experience as well.