I saw this Blogpost from Ikura about how they do testing in erlang and what they choose to test. I thought I would chime in on how I think about testing in Erlang.

First of all erlang hsa lots of tools for testing and validation type tasks. You can choose between Eunit, Common Test, for example based tests, triq, proper and eqc for property based tests. As well as dialyzer, xref cuter and more.

In a perfect world I would create property based tests with 100% code coverage, but in general that does not happen. so what do I do?

First of I use dialyzer a lot. And I like to use “tuple types” in my code base, So if I have a userid as a binary its type is -type(user_id() :: {'user_id', binary()}). .Then any functions that require a user id have a -spec on them and probably have a pattern match in the header. That way at least i know that bad data is kept outside of my functions.

I find dialyzer to be a mixed bag, it will tell you when something is wrong but the quality of the error messages is often horrible. I really wish they could be better, but when it says there is a problem you can count on the fact that there is one. Use Rebar3 to run it as part of your CI System.

I also consider a -spec() to be a test. if it goes red then there is something that needs fixing.

Secondly I like to keep my functions short. Shorter functions are easier to read, easier to test etc.

For actual tests I use a combination of triq and eunit. I don’t use proper because of the fact that is under the GPL, which is a shame, but there you go.

I have not yet had a chance to use Cuter, but I am sure I will sooner or later.

I find that this combination of things keeps my code “good enough” for what I am doing. If I had a large commercial project I think I would try to use CuTer and much more property based testing to really hammer the code.

Leave me a comment to let me know what tricks you use for keeping Erlang and Elixir running well

EDIT: Brujo wrote an article on how he tests his code in Erlang. His opinions are not the same as mine in all cases but go read what he wrote, and consider both. Bonus for his article I learned about a few new tools that I want to checkout like Gadget

I tend to not use Elvis for testing, but have all the various steps (Eunit, Dialyzer, QuickCheck etc) done by circle ci. That way I can also test any non erlang code that might be in a project like say frontend code in Elm. I don’t think either solution is wrong, but this is what works for me.

I have book on doing Property Based Testing in Erlang