Improving the ExUnit Infrastructure

Let’s learn how to modify the ExUnit infrastructure to cater to our tests.

Trimming the Infrastructure

Improving our setup is essential, but it’s not enough. We can also strengthen our tests with the following techniques:

  • Strip away ceremony.

  • Organize our infrastructure.

  • Use common aliases and helper functions that can quickly cut a couple of dozen characters in half.

To put it simply, these tiny bits of infrastructure will make it easier for our users to invoke the setups that improve our tests.

Typically, we’d add a few aliases to the top of a file and call it a day, but we’ll often have several different test files that need to use the same lines. Instead of tacking the same aliases to multiple files, we need a way to reuse these lines. Let’s take a deeper look to see how we’ll do that work.

Modifying the default test file

When mix new project_name creates a new project, it builds a test directory with two files. One is a simple test with a single line of consequence:

use ExUnit.Case

That statement is a macro that includes all the macros and functions our tests will need. One of the things that macro does is include the file /test/test_helper.exs. Let’s open it up and see what’s inside:

Get hands-on with 1200+ tech skills courses.