Defining the Unit in Unit Test

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

The Pragmatic Programmers
The Pragmatic Programmers

--

👈 Chapter 1 Unit Tests | TOC | Testing with ExUnit 👉

A unit test is a software test that focuses on a subset of an application, often removing the overarching context for how the code is used. It’s common to find a unit test focused on a single function and just the logic in that function, possibly encapsulating private functions available to it. Sometimes it makes more sense to include code from multiple modules, or processes, inside the scope of your unit tests, expanding the scope of the “unit.” Keeping a narrow scope can be beneficial to finding errors faster, but learning when it makes sense to expand the scope can greatly reduce the complexity of your codebase and your tests. If the scope of your unit tests, the unit itself, can be expanded slightly to include other modules, your tests can sometimes end up being simpler and easier to maintain while still providing all the benefits of a traditional, narrowly scoped unit test.

Before we dive into when and how to expand the scope of your unit tests, let’s touch on why we test. We need to keep four goals in mind when designing our tests:

  • Prove that your code does what it’s supposed to do.
  • Prevent breaking changes (regressions).
  • Make it easy to find where your code is broken.
  • Write the least amount of test code possible.

--

--

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.