Why Should I Write Unit Tests?

Daniel Stoica
Webtips
Published in
3 min readJul 23, 2019

Personal introduction

Since this is my first post on Medium.com, it’s probably advisable to start with a very brief personal presentation before moving on to the subject.

So, my name is Daniel Stoica, I am from Romania, but currently I live in the Netherlands and I have been working as Software Engineer for more than 10 years, focusing especially on writing end-to-end automated tests. I am also passionate about JavaScript and I am currently in the process of learning the React framework.

Enough with the introduction, more details can be found on my Linkedin profile.

Now let’s get to the subject of this post. So, if you are, or have been, a software engineer or a stakeholder for a software project, you probably wondered at least once by now…

Is it really worth writing unit tests?

Photo by NESA by Makers on Unsplash

Now, as I mentioned in my brief introduction, I’m not specialized in writing unit tests. However, having a role that means my primary responsibility is testing, I was asked this question or I was involved in discussions on this topic more than a few times, so I had the opportunity to look for and find answers to it.

First of all, the answer is YES. So in the following, I will list the reasons that I consider most relevant without claiming that the list is the best or the most complete. Actually, if you are reading this, please feel free to comment and give me feedback, even by suggesting what you think I should add to the list.

Unit tests allow you to discover software bugs very early in the development phase

Obvious, right? Maybe, but I think it’s important to emphasize the fact that the sooner you find a bug, the sooner and faster it is to fix it, without impacting other areas of the code.

Also, this reduces the cost of bug fixes, since finding a bug in a later stage, such as acceptance testing, means that the issue is most likely the result of many changes and it might take some time to figure out which one caused the current issue.

Unit tests allow you to make changes to your code much faster

Think about it, if your code is already covered by unit tests and you need to refactor it for some reason, you will immediately know if you broke something. Because you already wrote unit tests for that piece of functionality.

Unit tests document the code in an easy-to-understand manner

If you just started working a new project and are struggling to understand “what this piece of code is supposed to do”, I think one of the best ways to wrap your head around that is to go through the unit tests that cover that part. If those tests were written well, going through them should help you achieve that.

TDD improves code quality

You probably noticed this time I used “TDD” instead of “unit tests”. That’s because it is important to mention that this argument especially refers to unit tests that were written before the code itself. So why and how does this improve code quality? Well, simply because it makes you think hard about what problem you are trying to solve, therefore allowing you to anticipate issues and edge cases that might arise and make sure those are covered from the beginning.

TDD can help you understand when you should stop coding

Yeah, I know you love coding, but you need to stop at some point, right? Well, as long as you are confident that your unit tests covered the entire functionality of what you are currently building, then you can stop coding if all the tests pass. And move on to your next task. Or go home for the day. :)

That’s it for now!

That’s about it for now on this topic. There are other reasons that I can think of, but I don’t want to make this list longer and this post more boring than they already are. As mentioned before, feel free to drop a comment and let me know what you think. I will happily add any other reasons that you can think of regarding the importance and use of writing unit tests, just let me know why you think it’s relevant.

--

--

Daniel Stoica
Webtips
Writer for

Frontend QA Engineer with over 10 years of experience, currently based in Eindhoven, Netherlands. I like to write about testing and frontend development.