Should you write a test for your code?
We all know that writing tests are important, but it can feel like a drag to take the time to do it. After all, we’re developers and we want to spend our time building new features rather than writing tests. Many developers feel like writing tests is a waste of time because they don’t see the immediate benefits.
I would argue that the benefits are actually quite significant! Tests are an important part of the development process and they help ensure that your code is reliable and maintainable in the long run.
At the end of article, I’ll also share when writing test is not necessary
TLDR; Why writing test is important:
- It makes sure that your code base doesn’t break when you make changes to it
- It catches bugs early on so that you don’t have to find them later when it’s too late to fix them
- It helps with refactoring since you can see which parts are affected when you change one part of a function
- Save time by not doing the same test over and over again manually
- Save money because you can use that money to invest in growing your company instead of fixing bugs or hiring a tester (in the early stage)
How writing tests for your code can help you
Tests are important for any developer because they are the only way to ensure we are creating good code that is fully functioning at all times. So it’s a necessary evil that we should embrace rather than avoid.
Writing tests is vital for creating high-quality software, and it’s an essential part of any good development workflow. When we write tests for the code we write, we catch errors early in the process and prevent problems from spreading through the rest of our codebase. Not only that, but testing can also increase the speed of our development.
Sometimes, when we’re building a new feature (adding something to our existing code), it broke existing functionality due to changes that were made to the code without realizing it. Or we may miss a bug that we’ve introduced. In both cases, it’s important to have a safety net in place that prevents these issues from happening. That’s where tests come in.
Having a suite of tests can give you the confidence to make changes to your code without breaking things unintentionally.
When humans (Me and You) forgot or are just lazy to manually test all the functions, that’s when we need to write automated tests to make sure the new feature is working as intended and no issues were introduced by the new changes.
Test automation is the process of using computers to automatically test software - allowing programmers to test more code in less time.
By automating it, with just a little bit of effort, you can ensure that your code is tested automatically continuously. Now you have your robot QA tester that can run with one push of a button/command. Or even better, it runs every time you commit a new change.
Served as ‘Documentation’ for your team
Automated tests can be a valuable tool for collaboration, helping developers share their work with others and get feedback more easily.
Automation provides consistency among developers and helps ensure that new features are implemented and tested in the same way every time they are worked on. Since sometimes people don’t have some context when refactoring or changing existing code.
When you might not need to write a test?
Of course, as a developer, sometimes we want to just prototype some ideas we had or do a proof of concept. Or in the Startup world, when we’re building a Minimum Viable Product, a.k.a MVP. In these cases, it’s usually not necessary to write a test as we would just need to figure out our problem.