When I'm learning something new, these are the steps I take:
- Check the official language/framework for a tutorial.
- Make a "real world" project with what I have learned.
- When in doubt go to Stack Overflow.
In almost all the sites (if not all) they mention that adding tests to our applications is very important, so when I get to that part I get excited, I think Finally! I'll learn to add tests, but then I get there and start to think Wait, what should I test?, and then everything goes downhill and leave it there. So lets talk about test...
Why should we test?
At first, when I didn't know exactly what we were testing when adding tests, I though that they were only used to check that the code we were adding is doing what it is supposed to do, this got me confused a little I thought But if I write my test to pass the code I did, what is it testing?, but tests are not only for that. The most important thing about them is that we can be sure that if someday we refactor something or add a new feature, what was there it's still working.
- An API that returns an array of objects with two keys when called ️️️️️️✔
- A button that has only a string as a child element ✔
- Anything that our application is supposed to do ✔
I like to think that when we add tests we lock the amazing features we added on our app forever, and they will work as they are supposed to no matter what we do after.
Tests save you time
Yes! They really do. Sometimes we are refactoring our huge application and remove just one line of code and apparently nothing got broken, but then we push and deploy to QA/Stage/Prod or whatever you like to call it, and we receive a report that something that used to work isn't working anymore and we as professional developers do what we are supposed to do, panic and scream to the screen.
Imagine that before that we added some tests for that feature, we would be able to catch that before deploying, you can even add a process to check if all the tests passed before letting you make the commit and save hours.
Whether you are working by yourself, or in a team, on big applications test will save you a lot of time and a lot of headaches.
"But Carlos, sometimes we don't have time to add test, the project will be small, we won't touch that code again." Been there, done that. But from my experience on big projects testing would have save me a lot of time because I needed to check if I didn't break anything
When we should add tests?
I will say, whenever you like as long as you add them, there is this thing called test-driven development where you add test before anything, also you can create a test when you find a bug in your application and fix it, that way we can lock that and rest assured that this same bug won't come back again. I will say add them whenever you feel like, better late than never.
In conclusion, tests don't just help us to check that our code is doing what is supposed to do, but also it helps us knowing for sure that it will work as it's supposed no matter what, because if not the test will fail and we will know.
I know I have put the word tests so much that it started to look weird, but hey! We were talking about them, right?