Unit testing in Erlang using Rebar3 and EUnit
André Ferreira
Posted on December 22, 2022
Hello! In this article, let’s set up a simple project in Erlang with unit testing, following the basic steps of Test-Driven Development (TDD), using the Erlang build tool Rebar3 and the unit testing framework EUnit.
Motivation
I want to give beginners and professionals a primary starting point for developing in Erlang with testing. With more helpful content made about fundamentals of software engineering applied in a functional programming language, it can make the interest in this language rise and at least experiment with developing projects with the language.
Prerequisites
You must have Erlang and Rebar3 installed on your computer.
Creating a project
Let’s go! In the terminal, in a chosen directory, let’s create a simple project with Rebar3:
rebar3 new app simple_app
This will create a directory called simple_app
, that will contain the following content:
src/
simple_app.erl
simple_app_sup.erl
simple_app.app.src
rebar.config
.gitignore
LICENSE
README.md
Creating the test
Let’s start creating a simple unit test. First, we create a folder called test
inside the simple_app
directory. The directory should have the following structure:
src/
simple_app.erl
simple_app_sup.erl
simple_app.app.src
test/
rebar.config
.gitignore
LICENSE
README.md
Once created, we enter the folder and create a file called simple_app_test.erl
. In this file, we write the following code:
-module(simple_app_test).
-include_lib("eunit/include/eunit.hrl").
add_test() ->
?assertEqual(5, simple_math:add(2, 3)).
The project should now have the structure below:
src/
simple_app.erl
simple_app_sup.erl
simple_app.app.src
test/
simple_app_test.erl
rebar.config
.gitignore
LICENSE
README.md
Now, execute the test:
rebar3 eunit
The following output should appear:
Failures:
1) simple_app_test:add_test/0: module 'simple_app_test'
Failure/Error: {error,undef,
[{simple_math,add,[2,3],[]},
{simple_app_test,'-add_test/0-fun-0-',0,
[{file,
<test-file-location>},
{line,5}]},
{simple_app_test,add_test,0,[]}]}
Output:
Finished in 0.007 seconds
1 tests, 1 failures
===> Error running tests
As predicted, it failed. Now we go create the function add
to make the test work.
Making the test work
Let’s be organized and create the lib
folder in the simple_app
directory, where we will put the file with the add
function. The simple_app
directory should have the following structure:
lib/
src/
simple_app.erl
simple_app_sup.erl
simple_app.app.src
test/
simple_app_test.erl
rebar.config
.gitignore
LICENSE
README.md
Inside the lib
folder, we will create the file simple_math.erl
, where we will write the following code:
-module(simple_math).
-export([add/2]).
-spec(add(integer(), integer()) -> integer()).
add(Val1, Val2) -> Val1 + Val2.
Now, we need to get the lib
folder to be recognized by Rebar3 in order to make the test work. In the rebar.config
, write the following:
{erl_opts, [debug_info, {src_dirs, [“src”, “lib”]}]}.
{deps, []}.
{shell, [
% {config, "config/sys.config"},
{apps, [simple_app]}
]}.
{eunit_tests, [{module, simple_app_test}]}.
Finally, we execute the test again. The following output should appear:
Finished in 0.005 seconds
1 tests, 0 failures
That means the test has succeeded.
Conclusion
Thanks for reading! If you want to know more about unit testing with Erlang, check the section in the online book ‘Learn you some Erlang for great good!’ dedicated to EUnit!
Posted on December 22, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.