Unit tests were introduced about twenty years ago with the intent of improving software quality. They can be tedious, so developers (especially junior developers) tend to avoid them.
It is well known that there is a learning curve associated with writing unit tests, and mentoring is essential to build this skill.
While working in a team under heavy pressure to deliver, the project lead gave green light to the team to skip writing unit tests in the codebase, as developers were complaining that it was taking too much of their time to write them (and integration tests were easier, as doubles and mocks were not needed).
Despite this discouraging scenario, I kept writing unit tests because they help me write better code, and because I knew how important they were for the future of the system. Someday the code I wrote will have to be refactored, and the unit tests will help ensure that the code still works as expected.
However, the advantages of having unit tests were completely missed by the rest of the team! One huge but underappreciated advantage of unit tests is their ability to highlight flaws in our code due to poor design. When a piece of code is hard to test, that is usually a smell that the code is too rigid, it has too many dependencies, or it’s doing too much.
One of the reasons why the team felt they were safe only writing integration tests was our ninety-nine percent integration test coverage, but this is misleading. When we rely on integration tests only, it’s very likely that the overall code quality will be lower than it could have been if we had added unit tests leading us to write better code.
This is not about perfection, but about having code we can maintain in the future. There are many issues that become evident only when writing unit tests, and in their absence the issues accumulate until it’s too late to fix them, or too expensive. As I mentioned in my article about SOLID principles, this issue is “like smoking: having one cigarette doesn’t seem like a big deal, but the damage adds up.”
What We Lost
In summary, the value of unit tests is that they reveal problems in the code design, by making them obvious when something is too hard to test. They also help catch bugs in edge cases, and make it easier to refactor code with confidence. This leads to better structure, cleaner dependencies, and a more maintainable codebase.
Sadly, some of these benefits are only visible in the long term, so it is really hard to convince a team to get on board if they are under pressure and are unable to see that they are just making things worse for their future selves.
Looking back, I think we missed an opportunity by not encouraging junior developers to practise unit testing more consistently, as this skill really pays off in the long term. While I hope to be wrong, I find it very unlikely that they will be writing unit tests anytime soon after having the negative behaviour being unintentionally reinforced by a decisions made under pressure.
I understand the pressure of having to deliver code in tight deadlines, as I have experienced it myself multiple times. However, sacrificing quality should never a realistic option, and this is what discouraging unit tests achieves.
Instead, we should have encouraged the team to write more unit tests, and support them in this journey to learn the needed skill. We’ll always have tight deadlines, it’s part of the job, and we need to be able to manage projects effectively so developers can do a good job, which includes writing tests.
Final Thoughts
I wish this was an article sharing a great outcome, but instead, it reflects a moment when I couldn’t find the right way to communicate an important message in a way that made sense to everyone during a time of high pressure.
I never knew exactly how many bugs were released that could have been easily caught with unit tests, or how many design issues could have been prevented earlier in the development process. But I have no doubt that they were way more than they needed to be.
I hope this story inspires you to never sacrifice quality due to deadlines. If it helps you do the right thing in your team, then maybe I can get redemption for what I was not able to do with mine.
Happy testing!
José Miguel
Share if you find this content useful, and Follow me on LinkedIn to be notified of new articles.