Why Do We Need Unit Tests?

Learn about the importance of unit tests and how to write them.

We'll cover the following

What is a unit test? You can skip to the next section if you’re already familiar with unit testing and test-driven development. If not, let’s say that unit tests are part of an engineering philosophy for efficient and agile development processes. They add a layer of automated testing to the application code before it is developed. The core concept is that a piece of code is accompanied by its test, both of which are built by the developer who works on that code. First, we design the test against the feature we want to deliver, checking the accuracy of its output and behavior. Since the feature is still not implemented, the test will fail, so the developer’s job is to build the feature to pass the test.

Unit testing is quite controversial. While test-driven development is beneficial for ensuring code quality and maintenance over time, not everybody undertakes unit testing in the daily development workflow. Why is that? Building tests while we develop our code can sometimes feel like a burden. Especially when the test results become larger than the functionality they aim to test.

However, the arguments in favor of testing outnumber the arguments against it.

  • Building tests contributes to better code design. Our code must conform to the test requirements and not the other way around. If we try to test an existing piece of code and we find ourselves blocked at some point, the chances are that the code is not well designed and requires some rethinking. On the other hand, building testable features can help with the early detection of side effects.

  • Refactoring tested code is a lifeline against introducing bugs in later stages. Development is meant to evolve with time, and with every refactor, the risk of introducing a bug is high. Unit tests are an excellent way to ensure that we catch bugs at an early stage, either when introducing new features or updating existing ones.

  • Building tests is an excellent way to document our code. It becomes a priceless resource when someone unfamiliar with the code base takes over the development endeavor.

These are only a few arguments, but we can find countless resources on the web about the benefits of testing our code. If you don’t feel convinced yet, give it a try; otherwise, let’s continue with our journey and look at the overall form of a test.

The anatomy of a unit test

There are many different ways to test a piece of code. In this chapter, we will look at the anatomy of a test—the different parts it’s made of. To test any code, we need a framework for writing the test and a runner to run it on.

The test framework should provide utility functions for building test suites containing one or several test specs. As a result, unit testing involves the following concepts:

  • Test suite: A suite that creates a logical grouping for many tests. A suite, for example, can contain all the tests for a specific feature.

  • Test spec: The actual unit test.

We will use Jasmine, a popular test framework, which is also used by default in Angular CLI projects. Here is how a unit test looks in Jasmine:

Get hands-on with 1200+ tech skills courses.