Unit testing to cleaner code

Kristijan
3 min readNov 17, 2022

Introduction

Everyone will agree, software testing has an important place in software development. But why is that? The usual answer is that it helps us maintain the correctness of our software. That may be to test new code works as intended, but also that our new change didn’t cause a mistake somewhere in the previously existing code. However, that is not all. One often overlooked benefit is that it can help us write cleaner code, and in the rest of this article, you can read my thoughts on how unit tests help us do just that.

Types of testing

There are quite a few different types of testing. And when talking about testing, it is important to know which type we are talking about. Some types are:

· Unit testing — this type tests the smallest possible part of code independent of other areas. You give the required input and test what is the output based on that

· Integration testing — a type of testing where you take multiple different and connected parts of code and test how those work together

· End-to-end — this is the type where you would test the whole application with all its parts connected. That might include clicking along UI causing different interactions with the server and checking how it all behaves

There are more types but in this article, I am focusing on unit testing.

Unit testing

As mentioned above, unit tests are used for testing small and independent pieces of functionality. A function for example. You call a function with required parameters and observe result changes based on those given parameters.

To have good and efficient unit tests there are some things you might keep in mind. The first one, and maybe the most important one is to make functions pure.

If you are not familiar with the term, for a function to be pure it needs to satisfy two requirements:

1. Given the same set of arguments, the return value doesn’t change