Testing with ExUnit
Testing Elixir — by Andrea Leopardi, Jeffrey Matthias (13 / 80)
👈 Defining the Unit in Unit Test | TOC | Organizing Your Tests 👉
From the beginning, Elixir was developed with ExUnit, the test framework, as part of the core library. As a result, most of the test tooling we’ve come to utilize in our production applications is straight out of the Elixir core. This means that if you have Elixir and you’re familiar with what’s available, you can write effective tests without needing to bring in other libraries.
To learn how to use ExUnit, we’re going to start by writing our first test and then discuss some test theory, specifically the stages of testing. We’ll look at how to organize test files and then explore using common Elixir to help maximize the effectiveness of our tests. Finally, we’ll cover how to design code so that it’s well organized and easy to test.
Our First Test
Let’s write our first test in our rain alert app, Soggy Waffle. Soggy Waffle makes calls to an API and gets data back from it. It then standardizes the data to its own terms and data structures for the rest of the application to use. While those calls out to the API aren’t something we’ll focus on in this chapter (but will in Chapter 2, Integration and End-to-End Tests), the response transformation is a great example of code that can be tested in isolation. Let’s look at the code that we’ll be testing.