Why do we need to write tests
What is software testing? My interpretation of testing is it's a system to ensure our code still works with the addition or deletion of codes in an autonomous manner. Simply put, it is here to ensure our code works the way it's supposed to work. There are many different types of tests:
Test on the class or function level if output matches the expected output with given inputs. If it uses any external functions that cannot be controlled by input, we should mock it so our test result will be deterministic and not random. This includes getting fake response by sending a fake API request. These unit tests are usually run automatically before releases or when a pull request is opened.
It's like unit test but sending actual request to see if this group of system works together. Or it can be testing a group of UI components together to see if they interact correctly with each other.
This will interact with the UI to test if your UI works from the user's point of view and check if API responds correctly. This usually tests the defined user journey so for example for an e-commerce website we would test if user can navigate to product page, add product to cart, and then check out successfully. Since it usually take a long time and takes a lot of CPU power, we usually run it only certain time like once in 12 AM or run it manually.
Why bother writing tests?
Ensure our code still works
In the introduction I mentioned writing test is to ensure our code still works during the lifespan of our project. It is important so let me say it again.
New business feature comes and goes and our code evolves including changing the pattern, changing how things work, or changing the library. Tests are in place to ensure none of these changes destroy your application without you knowing especially if you didn't create this function in the first place. This is also the best time to think about what edge cases might happen and we can make modification to the source code as deemed necessary.
Tests usually have description that tells you what it is testing and what the expected behavior is. This can just describe what this function should do or how this certain feature should behave. I find tests are helpful in describing how different features should work. Of course in code you can add comments but they don't give you the full picture on how this class or method will be used. End-to-end test in which we test the user flow is especially helpful in this.
Have the tests run whenever we merge to release branch or whenever we open pull request, it gives us the developers more confidence that the application will be running fine. It gives us confidence to change the code and because tests will be run automatically thanks to CI/CD, you will soon get an email that you mess up if you actually did.
Recently, I say recently but it's actually been a year, my fantastic colleagues and I have been migrating the website from Java monolith to Vue Server Side Rendering with GraphQL as API server. Tests, especially end-to-end test, have helped us gain more understanding and clear some confusions on how things should work and what the user flow should be. We don't need to hunt down the design docs, project description, design mockups, or the people that made the feature. We check the tests and run them and see if tests still pass. This is much easier to do because tests are in the repo. We don't have to direct our focus on doing something else while we engineers can spend time working on our codes.
I know people want to release their features as soon as possible when they are done but ensuring your future self or your colleagues stay happy in the future are also important too. Good tests that serves as quality assurance and it gives engineers more clarities and confidences when making changes.
Are you writing tests or have tests helped you prevent disaster? Tweet to me to let me know!