The Pyramid of Unit Testing Benefits

I have had several "aha!" moments with unit tests, and have been burned several times by the lack of them. I have experienced how automated testing, especially unit testing, is a key building block for teams to iterate quickly and move fast. It's little wonder that all of the fast-moving tech companies I've worked at used this toolset company-wide - including Uber, Microsoft (the frequently-releasing teams), and Skyscanner.

While writing a testing section in my developer's career path book, I paused to think about the main benefits of unit tests, and how I see these build on top of each other. The first few benefits take short time to experience - validating your code is immediate - but other benefits take time.

It makes me wonder if the reason unit testing is more popular with more experienced developers is that they've worked long enough with codebases to experience the longer-term benefits, that compound with the short-term ones. Here's the list, with the stacked benefits of the unit testing approach.

  1. Validate your work. By writing a test, you double-check what you did. I caught so many mistakes this way - and I learned not to trust my own code, unless I double-check it.
  2. Separate concerns in your code. Unit testing your code requires you make it testable. Making code testable often means declaring dependencies upfront. This kind of structuring of the code usually leads to a cleaner design and better separation of concerns. With code that is not tested, it's easier to miss implicit dependencies and classes silently taking on multiple responsibilities.
  3. An always up-to-date documentation. While documentation will get out of date, unless you update it, tests that run and pass with the code will not get out of date. If you write clean tests, these tests can serve as evergreen documentation for the code. Fun fact: I usually start code reviews by reading the tests added/modified, to see if I can understand the changes just from the tests. If I cannot, that's usually a smell for me.
  4. Fewer regressions. I cannot count the number of times I made a code change that looked good to me - yet unit tests someone else wrote started to break because I introduced a bug. I also cannot count the number of times when we did not have unit tests for the code I changed, shipped the code, and discovered I've introduced a regression - weeks or months later. Unit tests do a remarkable job of catching regressions as long as they are in the codebase. Which brings me to the last point:
  5. A safety net for refactoring. While unit tests can catch regressions for small changes, they shine with large refactoring. In codebases with little unit test coverage, I have never felt confident making large refactors: simply because I have been burned by regressions upon regressions upon regressions so many times. On codebases with high test coverage and good tests, refactoring is much higher confidence work.

What are the benefits you've seen with unit tests short-term and long-term?

Featured Pragmatic Engineer Jobs

  1. Founding Engineer at Belfry. $140-180K + equity. New York City.
  2. Growth Engineer at Causal. Remote (Global)
  3. Founding Front End Software Enginee at Cradle. €75-160K + equity. Amsterdam, Delft, Zurich
  4. Senior Product Engineer at PartnerSlate. $100-180K + equity. Remote (Global)
  5. Site Reliability Engineer at Clarisights. €80-140K + equity. Remote (EU)
  6. Senior Software Engineer at Clarisights. €80-140K + equity. Remote (EU)
  7. Software Engineer - Product at loveholidays. £64-108K. London.
  8. Founding Engineer - Backend at Faliam. $150-200K + equity. US locations & Remote (US, Canada)
  9. Full-Stack Software Developer at Cutr. €70-120K + equity. Amsterdam, Remote (EU)
  10. Founding Backend Engineer at Flagright. €75-100K + equity. Berlin
  11. Founding Engineer at Mentava. $100-250K + equity. Remote (Global)
  12. Senior / Staff Software Engineer at Mainfold. Boston, MA
  13. Full-Stack Engineer at Bounce. €40-90K + equity. Lisbon, Remote (EU)
  14. Senior Software Engineer at Shogun. Remote (Canada, Global)
  15. Senior Backend Engineer at Gigs. €90-120K. London, Berlin, Remote (EU)
  16. Senior Frontend Engineer at Gigs. €70-110K. London, Berlin, Remote (EU)
  17. Engineering Manager, SRE and DevOps at Clipboard Health. Remote (Global).
  18. Backend Engineer at Paytient. $115-165K. Remote (US)
  19. Platform Engineer at Roar Studios. Remote (US).
  20. Head of Product Engineering at NearSt. £100-115K + equity. London.
  21. Software Engineer at CareerPlug. $130-155K. Remote (US, Canada).
  22. Full Stack Software Engineer at OpenTable. London.
  23. Senior Software Engineer at TwinWave Security. $150-200K. Remote (US).
  24. Principal Engineer at Clipboard Health. Remote (Global).
  25. Senior Java Engineer at Guestline. €60-84K. Remote (EU).
  26. Software Engineer - Backend at Bitrise. $100-240K + equity. Remote (US).
  27. Senior Software Engineer at Gruntwork. $170-203K. Remote (US, Canada, Global).
The above jobs score at least 10/12 on The Pragmatic Engineer Test. Browse more senior engineer and engineering leadership roles with great engineering cultures, or add your own on The Pragmatic Engineer Job board.