Unit Testing: Expectations & Reality
Story
Me, as a person who likes unit testing theoretically, never had a chance to write unit tests for a high-scale commercial app, but eventually got this opportunity and tried to do best about testing for the last 3 years, this is how it looks like from my own observation and experience.
Since software development supported by many visionary approaches, and people always seem to be liking it and get excited, but in reality this is not as easy as it sounds. Most known reasons that it is highly dependent on external factors such as product timeline, management decisions or even there is no answer for why you should adapt it. If you won’t have advantage of it and you can’t answer “why”, then don’t do it.
Well, what is all about unit testing stuff?
You have so many questions when you are beginner to it, right?
Where should I start, how can I write unit tests, what is asserting, mocking, stubbing etc. This is not the case for today. All these questions are just technical cliches, first we have to understand other perspectives and I will list my opinions respectively.
Developer Advantages
Unit testing is not just testing, whether you choose different approaches like TDD(Test First/Test Driven) or TLD(Test Last). It is changing your attitude of how you should code.
Remember all those patterns and principles? Even if you don’t know them, if you really understand why we are unit testing and shaping your code accordingly, well done!! You probably have a very good codebase because unit testing forces you to apply some of those basic principles naturally. Once you are good at it, you will realize your functions are well written, your classes are well decoupled and they are not god objects.
They are just pretty simple friends to maintain or refactor rather than monsters that you wouldn’t want to pass nearby.
So, with a well-covered and quality unit testing base, you can feel free to do everything in your codebase, because you know that if you break something it will catch you before it’s too late.
Product Advantages
Honestly, you won’t realize something really affects you more than just a little bit time consumption, sounds good? Wait, we have more. Assume that we got the optimum production quality with the codebase & unit tests also some other CI process, there will be less unexpected crashes or problems in production state. So this will make your users happy, so do you.
Disadvantages
Although there are big advantages, of course some minor problems might be waiting for you. Once you find yourself in a situation like immediate development required and you break your standard, it will follow you until complete refactoring of that function. Which is why try to do your work in a standard way and leave minimum technical debt.
Another thing that code coverage is good but not essential. When you force code coverage percentage, it may drastically reduce test quality.
Conclusion
Based on my experiences in a 10 yo project, 3 years with unit testing, I highly recommend unit testing and favor quality over quantity.
Try TDD before any important project because it can cost you significant time. If you are new to test, you should choose TLD to understand testing concept and lower your time requirement.
With a good scheme or review policy, it’s very possible to keep everything working on.
Once you realize you are wasting too much time on writing tests, that is a red alert you are definitely doing something wrong or complicated. Stop writing test, and think what is your mistake and refactor the code then write your tests from scratch.
I am planning to provide some detailed samples for the next post. Keep testing, stay safe.