Why do we need to write tests

Cover image

I have written 2 posts on writing unit tests in JavaScript to kickstart 2021. Some of you might wonder why bother writing code that doesn't ship to production, at least I did at some point.

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:

Unit Test

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.

Integration Test

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.

End-to-End Test

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.

Feature Documentation

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.

Continuous Delivery

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.

Remark

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.

Conclusion

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!