Home
Blog
Learn Elixir - From Zero To A Testing Hero

Learn Elixir - From Zero To A Testing Hero

・6 min read
Learn Elixir - From Zero To A Testing Hero

You may also like:

Why Choose Elixir For Your Next Software Development Project?

Read more

According to Marvel metrics, there is one Hero inside every local community. Thriving Elixir society offers space for more. Becoming a Testing Hero will not need a spider bite or a pocket full of kryptonite. How to become one? Discover and master the set of Testing Best Practices (TDD). It’s like approaching the next level of every game - from a random user who just plays a game to a Pro who creates the reality.

If It Works - Why Test It?

This question is old as the Stone Age. CTOs and Project Owners tend to look at the project from their own perspective. They want their product created at a fast pace and don’t regard testing as a priority repeating the old tune we can do it later. This usually means never. There is one basic rule for managing the development project. As long as you stick to it from day 1, you avoid pitfalls on D-Day - the finest hour when your first consumer lands on the shores of service page.

Test-driven Development in a Nutshell

Whenever you are leaving home to start a journey, you take a map to guide you around the unknown territory. It shows you all the right directions. The software development process has a similar manual named TDD. It is based on a recurrence of a brief development sequence. Its core element is unit testing. It allows you to test little pieces of code: a function, a module or a class. It then enables checking input in comparison to expected output and quickly discovers regressions. Moreover, it serves as documentation for future reference.

Think Before The Code

Like every method, TDD has its principles.

  • The tests should be written before the code. By these means, the test anticipates the right conduct of the code.
  • Next, we are allowed to proceed with only one test at a time.
  • Finally, the checks should be as brief as possible.

The whole process runs in cycles consisting of 3 stages:

  • Red
  • Green
  • Refactor

The Red phase includes writing the unit test which fails. The Green one is based on composing a code which repairs the fault allowing the software to pass the test. Refactoring basically means cleaning up the codebase and integrate the new units within. This usually involves improving the code readability or separating it into more logical parts.

Left map imageRight map image
Need help with your custom project? Schedule a consultation with an Expert Team.
Contact Us

Testing Best Practices in Elixir

Elixir has a built-in test framework ExUnit. It includes everything we need for writing tests while Mix is all we need to execute them. Tests are deployed as Elixir scripts so it is necessary to use the .exs file extensions. In order to start, we need to enter ExUnit with command ExUnit.start() using test/test_helper.exs. Let’s go back to Elixir School to learn the key ingredients of Testing Best Practices.

Assert

We use this macro to check whether the expression is true. In case of an error, the test will fail.

Refute

This negative assertion is used in the event we want to ensure an argument is always false or nil.

Assert_raise

We may want to assume that the error has been raised. In that case, we use the assert_raise macro.

Assert_receive

Applications consist of actors/ processes and they send messages to each other. This macro specifies whether the process received the message. You may set the timeout but you may also skip it with assert_received.

Capture_io and capture_log

This macro is used to capture the application’s output without any change to the original application. Capture_log may be used in the case of capturing the output into the logger.

Test Setup

There are cases where it is necessary to perform a set up before we run a test. Elixir provides us with 2 macros - setup which is executed every time prior to the test and setup_all that is executed once before the suite.

Mocking

According to the creator of Elixir, Jose Valim, using mocks can be harmful to the application design. This is why mocking is not welcome by Elixir community. There are situations when you have to use mocks though and in such cases, you may use Meck - a module mocking library for Erlang. Remember, you will need to add the macro to your dependencies first.

The Elixir essence - Useful Resources Online

Instead of giving you all answers on a single plate I gathered the handy websites in the form of All-you-can-eat Buffet. These tutorials, articles and technology dedicated services will make your test a better experience. They will hopefully trigger an appetite to explore this subject a little more.

The following materials will surely become your testing buddies. The first one is ExUnit Official Documentation. The next is a chapter titled Finding Zen Through Testing from the book Mastering Elixir. For Phonenix lovers, there is a brilliant publication Programming Phoenix. It contains all necessary information about testing and a lot more for curious readers. Another book titled Craft GraphQL APIs in Elixir with Absinthe provides a testing guide for a GraphQL APIs build with Absinthe.

Finally, if you haven’t added Thinking Elixir, Elixir School, Forum, GitHub Elixir Tips or Hexdocs Testing tutorial to your bookmarks, it’s high time to do it.

Wrapping Up

This article has one simple purpose - to introduce you to testing practices in Elixir. I already told you about the fantastic solutions for building scalable applications. I shared my view on exploring new technologies and learning new languages. My most recent article allowed you to read the love story about big market players and their chemistry for Elixir. This time I took you to the next level of functional programming. I chose to focus on intuitively simple manual and a set of practical tips. Just like with a pack of digestive biscuits, you may take one at a time or munch all at once. Either way, the information will proceed smoothly through your organs.


Rate this article:

5,0

based on 0 votes
Our services
See what we can create for You
Our services