Are unit tests important ?

Tejanvm
2 min readSep 29, 2023

--

I’ve seen both sides.

If your code base looks like noodles, and no body wants to waste time writing unit tests, the tests in this scenario doesn’t help. Instead focus on improving your code base. Once you think you have separated the model and service and UI layers, and you believe you are using the singletons correctly, it is time to put some of that effort into writing unit tests.

Do not write unit tests just for the sake of increasing the code coverage. You should aim to write unit tests to cover your public interfaces.

Do I think unit tests are important? Let me tell you my experience.

I was working on a logic to tell a particular grocery store was open or not. I will not go into details of how the logic is implemented. The backend will send us a formatted string “9AM-4PM” for each grocery store. It is upto the frontend to build the logic around this.

The logic that is inplace worked fine, or atleast that’s what we think. We even had some unit tests to test this logic. The tests pass all the time, or atleast that’s what we think. Until we found the tests were failing in some rare scenarios, like tests were failing every time they were run after 12pm.

There is one particular test, it is using a formatted string “2PM-3PM”, in general scenario, what grocery store would be open only for 1 hour? Well the unit tests can be a great place where you can make those scenarios, this particular test exposed a major flaw in the logic that tells if a store is open or not.

Someone can argue, you could have written a better code in the first place, well who doesn’t want that, it is great if you can do that, but time to time we loose track of the logic, and only thing that assures us that our logic works are these unit tests. They are our friends not our enemies and friends are important!!!

--

--

Tejanvm

I'm an iOS Developer. I love to learn new things in programming and share my knowledge.