Before I started my 3rd internship, I have no idea what unit test is, and why is it relevant. But after I’m forced to write unit tests for half a year, I realize how important it is. Apart from making sure your functions do exactly what they’re intended to do, unit tests also provide the following benefits:

Keep you from breaking existing features

Sometimes when you’re developing a new feature, you will change the underlying structs or even directly modify some existing functions. Fortunately, their corresponding unit tests will tell you if you’ve broken anything.

Force you to go through your own code again

After spending hours or days on a new feature, you can finally submit a PR! But wait … you haven’t written any unit tests for the new functions yet. With a new code coverage of 0%, your PR is blocked, so you’re forced to write unit tests. But when writing a unit test for a function, you’ll need to know exactly what the function should do, what the input and output are, and what external functions will it call so that you can mock them. Therefore, unit tests force you to go over your code again. Chances are, you’ll find some spaghetti code you wrote when you were just focusing on making things work.

Stop you from keeping unused/obsolete code

During the development process, you may write some functions only to find out they are not needed afterward. But hey can I just keep it there? I don’t want to kill my baby! No, because that will lower your test coverage, unless you write a unit test for it … but you’re too lazy to do that, so you just delete it.

There are also times when the specs are changed, new functions are created, and the old functions become obsolete, relying on obsolete structs or methods. Can I keep it? No, because their once glorious unit tests providing 100% coverage just become the failures on your CI build now. So I delete them.