Test Driven Development is the idea that by writing the tests for software before the implementation of any executable code, we can ensure maximal test coverage and code quality. The following blog outlines the process of test driven development as a how-to/guide, as well as benefits to a test driven development process.
Benefits of Test Driven Development
- Extensibility and Maintainability -Traditional styles of development were faced with several challenges around extensibility and maintainability. Because test-driven development is ingrained into the development process at such a granular level, it is guaranteed that every standalone piece of code can be changed without risking adverse effects to other pieces of the software.
- Improved code quality with refactoring - Refactoring is a central process with test driven development. It ensures that developers are always strengthening their code base which will ensure that the software is up-to-date and modular.
- Simplification - Writing tests first allows software engineers to really consider what they want from the code and the functions that it will provide. It forces engineers to only right code in response to the requirements of the tests they have written.
Seven Steps of Test Driven Development
- Before writing any code, devise a use case for the part of the software that you are building. It should be clear to an end-user whether or not a test passes or fails. It can be helpful to devise use-cases like this in the form of behavioral types of verify statements. For example, an easy model to use would be in the form of a "given, when, then" model. (Ex. Given: The Temperature 32°F is in the conversion field. When: I click the "Convert to Celsius" button. Then: The temperature in the conversion field changes to 0°C)
- Write the unit test for your use-case. Do this before writing any implementation of the code for the use-case. Then, run the test to confirm that it fails. Yes, it will fail at first, but that is the idea. If it doesn't fail at first, that means something is definitely already wrong, and you may be duplicating already existing code or using variable names that are the same as somewhere else in the program unknowingly.
- Write only enough code to cause the test to pass, and no more than that. After your test consistently passes, your are able to move on to writing a test case for the next use-case.
- Write a test for the next use case in the same manner as the first test case. In this way, we are building up a test suite that grows organically with the software to cover almost all executable code.
- Write only enough code to cause all existing tests, and your newest test to pass, and no more than that.
- Refactor your code to clean up your code base as needed.
- Repeat steps 4-6 until the story you are working on is complete. Congratulations! You have completed a story using test driven development methods.
Software engineers at AndPlus utilize test-driven development in our agile development process. If you would like to learn more about agile and how we utilize test-driven development in an agile framework at AndPlus, download our Agile 101 Handbook by clicking the button below!