Organizing Your Tests

Testing Elixir — by Andrea Leopardi, Jeffrey Matthias (14 / 80)

The Pragmatic Programmers
The Pragmatic Programmers

--

👈 Testing with ExUnit | TOC | Creating Comprehensive Test Coverage 👉

As well as making sure our code works, our test suite serves a secondary purpose. If written well, it becomes a source of documentation for our code, with text descriptions of how we expect the code to work and working examples of how to exercise the code. This sounds like a lofty goal, but with a little organization, it’s entirely achievable. Imagine a future you looking back at code that you haven’t seen in months and trying to remember how it works and how to use it. The more organized your test suite is, the more future you will love past you for reducing the complexity of this challenge. That alone can serve as motivation to think about the overall design of your tests. Fortunately, ExUnit ships with some useful functions (and macros) that allow us to organize an individual file in a way that’s readable and maintainable.

As we build more and more complicated tests, you’ll see these organizational tools showing up in our examples. Let’s start by looking at those tools and code examples and how you can use them to keep your test suite easier to read and maintain.

Describing Your Tests

While it’s possible to have a readable test file with a flat organizational structure, ExUnit gives us a good tool for grouping tests together within a file, describe. This tool allows us to pass a…

--

--

The Pragmatic Programmers
The Pragmatic Programmers

We create timely, practical books and learning resources on classic and cutting-edge topics to help you practice your craft and accelerate your career.