As I mentioned in my email yesterday, there is so much misguided advice in the community about unit testing.
A lot of developers are against unit testing, or they simply take the "abstract and mock everything" approach.
In this email, I’m going to help you debunk the 4 most common myths about unit testing.
Myth 1: Unit testing is a waste of time
It’s true that unit testing doesn’t help you at the beginning of your project.
Here’s a graph that demonstrates this very well:
A project without tests has a head start but quickly slows down to the point that it’s hard to make any progress.
Unit testing saves time in the long term: it allows you to keep the steady pace of development.
You don’t need to worry too much about bugs you might introduce when writing new functionality. Tests help you reveal (and fix) those bugs at early stages.
You also become much more confident when refactoring your code, since you know that the tests got you covered. Continuous refactoring is a must to keep your project maintainable.
I understand, it’s natural to resist unit testing, but the time savings are worth it in the long term.
Myth 2: Unit testing is complicated or takes too long
Well, when you first start writing unit tests, it probably will feel complicated.
Just like the first time you learn to drive, it could be overwhelming as well. The gas pedal, brakes, turn signals, rear mirror… Everything is new.
But once you get over the initial learning curve, driving becomes a lot faster. And it opens a new world of opportunities that you can explore freely.
Unit testing is the same.
With experience, your time spent on writing tests decreases drastically, improving the return on your efforts.
Myth 3: You only need unit testing when there are many developers
Unit testing can help a one-person team just as much as a 40-person team.
It helps the developer to keep the pace of programming constant and introduce new functionality almost as fast as in the beginning of the project.
All the benefits of unit testing equally apply to large and small development teams.
Myth 4: Unit testing automatically leads to better code design
There’s a common misconception that unit tests automatically improve code design.
It is true, but only to a degree, and that degree is much smaller than a lot of people imply.
Think about it this way.
The ability to unit test a piece of code is a nice litmus test, but it only works in one direction.
It’s a good negative indicator — it points out poor-quality code with relatively high accuracy. If you find that code is hard to unit test, it’s a strong sign that the code needs improvement.
The poor quality usually manifests itself in tight coupling, which means different pieces of production code are not decoupled from each other enough, and it’s hard to test them separately.
Unfortunately, the ability to unit test a piece of code is a bad positive indicator.
The fact that you can easily unit test your code base doesn’t necessarily mean it’s of good quality. The project can be a disaster even when it exhibits a high degree of decoupling.
Conclusion
So there you have it, 4 myths about unit testing debunked.
To sum up, unit testing does require more time in the short term, but it can save you hundreds of hours on manual testing.
And if you follow the 4 pillar structure in my book Unit Testing Principles, Practices, and Patterns, writing unit tests becomes simple and clear.
This book is a culmination of my over 15 years of research, trial, and error — compiled, refined, and distilled.
If you don’t have much experience with unit testing, you’ll understand what makes a good unit test and how unit tests enable sustainable growth of your project. You’ll see which tests contribute to the quality of that project and which must be refactored or eliminated.
If you’re an experienced programmer, this book will help you look at unit testing in a new light. It will help you articulate why the techniques and best practices you’ve been using all along are so helpful. You’ll have a logical foundation behind unit testing best practices.
On Amazon, you can find tons of positive reviews about this book. A professor even used this book as the textbook to teach unit testing.
So, if you want to write great unit tests with minimum pain and avoid all the trial and error that I’ve gone through…
(Again, the discount code is nwsentr40.)
Take care,
Vlad.
P.S. After you grab the book, I’ll give you another bonus.
It’s my course, 5 Non-Obvious Tips for Writing Better Unit Tests.
All you need to do is forward the book purchase receipt to my email book@enterprisecraftsmanship.com, and I’ll enroll you in the course as a bonus.
P.P.S. If you really like the book and want to consult with me, reply to this email with the word "consult", and we can chat more.