What is Testing
Testing is a critical element in software development that aims to evaluate and ensure that a software system or application works as planned. The primary purpose is to detect problems, errors, or bugs in software before it is made available to users. Various procedures, techniques, and types of testing are used to validate software against specified criteria and ensure its quality, reliability, and performance.
What is Test Driven Development
TDD is a software development approach in which tests are developed before the real code. TDD follows a cycle of developing tests, implementing code to pass those tests, and then changing the code while ensuring all tests continue to pass. This cycle is also known as the "Red-Green-Refactor" cycle:
1. Red: A test is initially written for a specific piece of functionality that does not yet exist or for which no code has been written. Because the associated feature is not provided, this test is predicted to fail (the "red" phase).
2. Green: The developer produces the bare minimum of code needed to pass the test (the "green" phase). The code is often written to pass the test, rather than for any other reason.
3. Refactoring: Once the test passes, the developer refactors the code in order to improve its design, maintainability, or performance while ensuring that all tests continue to pass. This ensures that the code is clean and efficient.
Benefits of Test-Driven Development and why to adapt it
1. Tests as Documentation: Tests function as a type of code specification and explain how the code should be used.
2. Incremental Development: The development process is carried out in incremental steps. Each new feature is introduced one at a time.
3. Immediate feedback: If a modification disrupts existing functionality, the automated tests will detect it right away.
4. Code Quality Improvement: TDD frequently resulted in cleaner, modular, and more maintainable code.
Example of Test Driven Development
Step 1: Write a Failing Test
Let's say we want to create a simple Calculator with division functionality. We'll use JUnit for testing.
Step 2: Create the Class
Now, we create the Calculator class without any method implementation. Running the test will fail as the method divide() does not exist in the class.
Calculator Class (Empty)
Step 3: Implement Just Enough Code to Pass the Test
Add the divide() method to the Calculator class to make the test pass.
Calculator Class (With divide method)
Step 4: Run the Test
Run the JUnit test CalculatorTest. It should now pass because the divide() method has been implemented in a way that satisfies the test condition.
Step 5: Write another failing test
Add another functionality by creating failing test.
How the calculator behaves when zero will come as divisor.
Step 6: Refactor
Now refactor the code to include the functionality to handle the case when divisor is zero.
This procedure exemplifies the TDD technique by designing a failing test, implementing the bare minimum of code required to pass the test, and then modifying the code as needed. It guarantees that every code is tested and verified from the start of the development process.