Testing is necessary to ensure that everything works as expected.
And, to be honest, we all kinda do it when we are developing. Or am I the only one who uses a bunch of
fmt.Println or anything like that everywhere?
For example, when we are developing an algorithm we create variables containing the inputs of the use cases that we want to make sure they work. It’s very instinctive. What testing propose is to systematize that.
In the process, you will write a little more code, yes, once. But then, what you’ll get in return is the reliability of your code; it will force you to write more modular code and clean architecture; you can refactor knowing that you need to make sure to pass the tests that you already created; and really put your code to the limit trying all kinds of different edge cases, all while ensuring that their processes do not disrupt development.
Wait… if it is that good. Then, why everyone is not doing it?
True, it sounds too good to be true, right? Well, because it is. You see, although I said that we instinctively already do testing, it’s not like learning and using the tools are trivial things. As with any skill it requires time and practice to master it. Writing good tests is a skill all by itself.
And even when you manage to get the hang of testing, making tests takes time and effort and, most important of all, writing tests don’t assure that no bug will be introduced into the code base. I’m afraid that we don’t write tests for our tests so we can’t be 100% sure that every use case was tested.
Alright, I get it now… then, why even bother?
Sure, there’s the usual: “caching bugs early” and all things I already mentioned. But, from a developer's point of view, it leads to more reliable and modular code and clean architecture, which can make it more maintainable, scalable, and reusable. By systematically testing your code, you can ensure that it works as intended in a variety of use cases.
Also, I would say that while testing you get to know a little bit more about your components. You can experiment with different scenarios and see how far can they go; and refactor when you see that it’s not working as intended (whether the code or the tests themselves).
Finally, It’s not only to debug an application but also a culture, a way of approaching the development process. Testing sets the standard of the application.
Alright, you convinced me. Where do I start?
Well, it will depend on what languages and frameworks/libraries you use. But, here are some principles that I use to write tests:
Logic and UI separation
Sometimes it's easier to test your components in their different functionalities and then test them together than testing all at once and trying to figure out where a possible bug could be.
Start with the basics and move to the complex ones
Sometimes time we forget to check if the code we want to test sends a response to begin with, or even if it is even defined. So, test even the most fundamental things.
Keep it short and sweet
Try to test one thing at a time.
Don't forget to test edge cases and error handling
It's important to test not only the expected results but also the unexpected ones. By testing edge cases and error handling, you can ensure that your code is robust and can handle unexpected scenarios.
Use mocks and stubs
Mocks and stubs are tools that can help you isolate and test specific parts of your code. For example, if your code relies on an external API, you can use a mock API to simulate different responses and test how your code handles them.
Every failing test is an opportunity to improve
Yes, that’s right, the idea is to improve code quality, so, use every failed test to solve it and hence improve the code and reliability of it.
Don't test everything
Testing is important, but it's not realistic to test every single line of code. Instead, focus on testing the critical parts of your code and the parts that are most likely to break.
By following these principles and incorporating testing into your development process, you can build more reliable and maintainable software.
Find your own thing
Even when you learn from courses, coworkers and tutorials, it’s also important to listen to your own experience and find your way to do it.
I’ll be releasing an article on testing in react native with this example that I made only for that article. Here’s the repository.
I don’t know if I’ll gonna release it after this article because I’ve been organizing my study notes and stuff. But I’ll let you know 😆 (And, yes, I did the thumbnail of this article. Pretty cool, right? 😎).