Birth Control for Bugs: Test-Driven Development
Development is hard, not only does it take time and effort, the programming aspect has so many components where things can go wrong. We’re human, we’re bound to make mistakes, that is why coding bugs exist. Adding to that, working in a team means there will be multiple people making these mistakes, creating a bigger and messier mistake. In spite of that, development doesn’t have to be hard, it could be less hard. Something I was taught about around last year was a development method called test-driven development, and honestly, it made my life harder, but more than that, it made development easier, especially when working in teams.
What is Test-Driven Development?
Test-driven development, or TDD for short, is a software development method where programmers write automated test cases before writing any code. This method keeps track of software development by frequently testing it against all test cases. The development approach, created by U.S. software developer Kent Beck, outlines and tests how a code will function by developing test cases.
The creation of failing tests for a piece of functionality under development is the first step in the development process. These tests are nothing but requirement conditions that must go through testing to be accepted. Writing the tests first forces proper thinking of what to expect in the code.
Then, a simple code is written to implement that functionality to pass the failing test. Because a tiny piece of code is written at a time to pass the tests, this helps avoid code duplication. This process generates a detailed specification and confirmatory testing for the code.
Once that is done, that code is refactored to meet a set standard. The unit tests created initially are straightforward and serve as documentation for the code. Hence, other programmers can regard the tests as usage examples of how the code is supposed to work because TDD use cases are written as tests.
Q: When do testing is required in the process of implementing a functionality?
A: No. Companies usually aim for a code coverage of around 80–90%. For a big long-term project, this leaves some wiggle room for changes to be made without breaking the system.
“Don’t try to achieve 100% code coverage. There’ll be little value in that last 5% to 10%, which makes it a waste of time, effort, and money.”
“For most applications, a code coverage of 80% to 90% is a very good score.”
— Peter Morlion, LINEARB
TDD in Badaso LMS
At the beginning of the sprint, each member of the development team was assigned a feature (PBI) to implement. In the respective feature branches, we are meant to practice TDD in the development of that feature, marking stages of the TDD cycle with certain tags in the commit message: [RED], [GREEN], [REFACTOR].
At the start of this project, we were not very familiar with the stacks yet, so writing any code at all was rough, let alone creating tests. This issue made practicing TDD became a bit of a setback. However, after getting used to both Laravel and Vue, having tests already made for each bit of functionality sets a clear guide in the development process. Here are some snippets of what the test and code looks like in the commit example:
Following the tests, an “Add Course” tag is on the card along with the two buttons, “Create” and “Join”.
I hope you find this blog helpful, if you find any mistakes or misinformation, I would very much appreciate the feedback. Thank you!
References
Hamilton, T. (2022, February 26). What is Test Driven Development (TDD)? tutorial with example. Guru99. Retrieved March 19, 2022, from https://www.guru99.com/test-driven-development.html
Novoseltseva, E. (2017, March 2). Benefits of test-driven development — dzone devops. dzone. Retrieved March 19, 2022, from https://dzone.com/articles/20-benefits-of-test-driven-development
Unknown. (2021, June 29). What is TDD– test driven development. Innuy. Retrieved March 19, 2022, from https://www.innuy.com/blog/what-is-tdd-test-driven-development/
Ivo, A. A., Guerra, E. M., Porto, S. M., Choma, J., & Quiles, M. G. (2018). An approach for applying test-driven development (TDD) in the development of randomized algorithms. Journal of Software Engineering Research and Development, 6(1). https://doi.org/10.1186/s40411-018-0053-5
Morlion, P. (2022, January 14). Code coverage types: Which is the best? LinearB. Retrieved March 20, 2022, from https://linearb.io/blog/code-coverage-types/
Attached illustrations were made by me :)