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, Front End and API at Causal. $175-225K + equity. Boston.
  2. Senior Software Engineer at Portchain. Coppenhagen or Remote (EU).
  3. Engineering Team Lead at Portchain. Coppenhagen or Remote (EU).
  4. Software Engineer - Infrastructure & SRE at Koyeb. €71-105K + equity. Remote (Global).
  5. Software Engineer - Service Mesh & Networking at Koyeb. €75-105K + equity. Remote (EU).
  6. Security Lead at Conjecture. £85-210K. London.
  7. Senior Software Engineer at Consensus. Boston, MA or Remote (US).
  8. Senior Frontend Engineer at Plain. £75-110K. Remote (Europe / 2 hours within GMT).
  9. Lead iOS Engineer at Polarsteps. Amsterdam or the Netherlands.
  10. Distributed Systems Staff Engineer at Reframe Technologies. Remote (Global).
  11. Senior Software Engineer at Karat Financial. $160-225K. Remote (US time zones).
  12. Senior Backend Engineer - Ruby at Rise Calendar. €80-120K + equity. Remote (Europe). I'm an investor.
  13. Senior DevOps Engineer at Droit. $170-205K. New York.
  14. Senior DevOps Engineer at Droit. London.
  15. Full Stack Engineer at Applied. £45-69K. London or Remote (UK).
  16. Solution Engineer at Pigment. $90-180K. New York or Toronto.
  17. Senior Backend Engineer at Pigment. €60-130K. Paris.
  18. Senior Engineering Manager at Whimsical. $185K + equity. Remote (US, Canada).
  19. Engineering Manager at Bobsled. $170-190K. Remote (US, Europe, South America).

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 and apply to join The Pragmatic Engineer Talent Collective.

Want to get interesting opportunities from vetted tech companies? Sign up to The Pragmatic Engineer Talent Collective and get sent great opportunities - similar to the ones below without any obligation. You can be public or anonymous, and I’ll be curating the list of companies and people.

Are you hiring senior+ engineers or engineering managers? Apply to join The Pragmatic Engineer Talent Collective to contact world-class senior and above engineers and engineering managers/directors. Get vetted drops twice a month, from software engineers - full-stack, backend, mobile, frontend, data, ML - and managers currently working at Big Tech, high-growth startups, and places with strong engineering cultures. Apply here.