Why I Write Unit Tests
Over the years, I’ve tried various methods of writing tests for the code I write. Some folks prefer to write tests first, and some prefer to tackle them last, if at all. In the end, I think it’s critically important to write tests whenever possible, but how you do it is entirely up to you and how you work best.
In this article, I want to talk about a few benefits of writing tests that may not be immediately clear to someone who may be on the fence or has already decided that they are a waste of time. I used to believe they were just something that opinionated developers enjoyed flipping tables about, but that didn’t ultimately matter much. However, once it was forced on me, it didn’t take long for me to see and start advocating for their addition to any serious codebase.
1. Tests Result in More Efficient Code
When you start by writing your tests up front for any task you find yourself working on, you will often find yourself asking a lot more questions than normal about how each particular piece needs to function and look. This is excellent because it forces you to have a more robust scope of the work before you start coding. Additionally, it helps you not forget about certain edge cases in the process.
As a result, your code is more likely to be more efficiently written and easily maintained.
2. Tests Improve Quality Control
At companies I’ve worked at in the past, we had employees whose job it was to test all the features developers had written. While a QA team is still a great thing to have if your budget can afford it, in our case, a lot of the bugs they found were things that tests would have prevented.
However, when your code is test-driven, your QA team is often likely only to find CSS issues related to browser intricacies. Because you’ve written tests that ensure the desired functionality, the only functional issues are going to be things you didn’t think to write tests for (which can be quickly remedied).
3. Tests Prevent Breaking Changes
Have you ever implemented a feature in your app only to find out it broke something else? Have you ever deployed that feature without realizing that a new bug was present?
Writing thorough tests in your application will make you aware of when something breaks. As a result, you will be able to catch mistakes, preventing users and clients from being the ones to do so.
4. Tests Protect Your Reputation
How many applications with great ideas are now extinct because of countless bugs in the end product? Quite simply, too many to count.
By writing tests that verify functionality and enable you to catch mistakes before they get deployed, your users and clients will be less likely to find bugs in your work. As a result, your reputation will be that of a reliable developer, and your apps will be both less likely to break and more likely to succeed.
5. It’s How You Already Think
Lastly, a coworker of mine once made a profound statement about writing tests. He said, “You already write tests in your head, so why not actually write them in your code?“.
He meant that when you look at a feature you need to write, you start by asking questions like, ”What does this need to do?” ”What result do I expect?” or ”What happens if I use it incorrectly?” What you may not realize is that the answers to those questions are the script to use in your tests! So why not use those scripts, and take the time to make sure that you deploy works the way you expect it, which will also ensure it doesn’t unexpectedly break in the future?
Go forth
Now, I do not claim to be an expert in Test Driven Development, but the more I do it, the more I enjoy it. In some ways, it makes my work feel like a little game of “Let’s make all the tests pass,” and when that happens, my work is done, and I can move on to the next item on my list.
If you haven’t taken TDD for a test drive yet but are interested in doing so, look for some good articles on getting started using your development stack. If you have any questions or need some pointers, please contact me. I’d love to chat!