Basic Unit Testing In Elixir

One thing that every programmer must do is test their code. Many languages have ways of testing them, some hard to use some easy to use. Elixir is most certainly on the east end of the scale, with its built-in framework called `ExUnit` it allows you to easily set a file as an executable. All executable tests are called with a simple command `mix test` and you're off, hundreds of green dots flood your screen, ecstasy!


Let's set up a simple test to run through the testing process then I’ll discuss how we used this testing framework with our project Class Solver to create robust and reliable code.

First thing you want to do is create a project. Simply run `mix phx.new <project_name>`

ink.png


Done your project is ready. Change directory into the new project and then run `mix test` this will do an initial compile and then run some tests

ink (2).png

Look at that, 4 green dots, 4 successful tests, no failures and you even get the time to complete. One thing you should know about how Elixir does it’s testing is that it does them concurrently (off by default) so each test CAN’T rely on another test otherwise your going to get some pretty weird behaviour. Furthermore, each test run will run them in a different order, hence why it spits out a seed number at the bottom, this number can then be used to rerun the exact order if you by any chance needed to run that exact order again.

ink (1).png

As you can see it uses the `ConnCase` module which is used to do connection based testing. What's very important is setting the `async: true` flag, this allows the tests to be run in parallel.

Alright, let's move on to our own tests. In your test folder create a new folder with the name of your project with a trailing “_api” like so:

ink (4).png


This is where you're going to house the API tests you create. Something to note about the tests files is that there all executables, so instead of using the traditional “.ex” file extension we use “.exs” this tells mix that there executable files.

This is how you set up a basic test when you're testing a function that doesn’t talk to any sort of database or do any sort of authentication.

ink (5).png

Lets breakdown what's in this, first off you define the module like you would any other file, then you declare that you want to use ExUnit.Case which configures and prepares them for testing.

Each test is declared by calling “test” then naming it inside a string. Then you simply but what you want to test inside the function, then test that it works and you're done. More green dots! one does, in fact, equal one.


Testing in Elixir is a first class citizen, just like it should be. You get it out of the box and often when you use the generation tools provided by the Phoenix framework they will also generate a whole bunch of tests for you as well. This makes it extremely easy to get up and running with a test framework, you just generate them and edit them as you go and you’re done.

Testing is one of my favourite things to do and Elixir makes it so easy that even someone who hates it will get by.


I’m always learning something new and with Pluralsight I can have unlimited access to all things programming. But hold on, it’s not just programming. There’s photoshop, visual communication, manufacturing and design. There's a whole bunch including Elixir! Sign up today and get a free 10-day trial!








Previous
Previous

GraphQL Queries with Node

Next
Next

Think about the whole stack