Test Driven Development is not just a software development process, it’s a basic software hygiene.

Test-Driven Development is an iterative process where the developer writes the test cases before they write the production code. It’s always said that TDD is an error-driven approach where new code is written only if the existing test cases fail. It’s a rapid cycle of testing, coding, and refactoring.

Image for post

## Step 1 -> Write a test, watch it fail

The first step in TDD is marked as a RED zone area, here a developer is expected to write the unit test for the function which s/he will be implementing. This test needs to be very specific to a single functionality. Developer is expected to write the test case and run the same to get the possible errors if any. Guess what, this will definitely fail but will assure you that you’re calling the right methods which need to be developed.

Developers might find this as a time consuming effort where it’s obvious that a test case will fail before the actual code is written. However, think of this as a design phase. We won’t know what to implement until we know how it needs to behave at any given instance.

Now let’s take an example of array sorting. We start by making an assumption for a main function and define the input and output state after the invoke as below;

Integer[] inputArray = { 8,7,2,1 };
Integer[] outputArray = { 1,2,7,8 };
sortArray(inputArray);
assertArrayEquals(inputArray, outputArray);

When the developer runs the test case he will receive basic errors like function not defined, don’t worry as this error is expected as the main function is not defined.

## Step 2 -> Write just enough code to pass the test

This zone is basically a GREEN zone where the actual functionality is being implemented to pass the test case. The developer needs to write just enough code for the test to pass. Don’t add or change the test case to pass the test, this will assure that the initial functionality discovered will be achieved. Run your test again / modify the code till the test passes and a green bar is displayed for the test case.

In the above example, the sortArray (); function needs to be implemented. A developer will make a decision on function structure like what internal variables to be declared, which loops to be used etc. Remember the words “Just Enough Code”, we will use this as a coding principle to develop the functionality.

for (int i = 0; i < sortArray.length; i++) {   
   for (int j = i+1; j < sortArray.length; j++) {   
      if(sortArray[i] > sortArray[j]) {  
         temp = inputArray[i];  
         sortArray[i] = sortArray[j];  
         sortArray[j] = sortArray;  
      }   
   }   
}
return inputArray

## Step 3-> Improve the quality without changing the code behaviour

Refactoring is still a part of the GREEN zone where developers will modify the code without affecting the functionality. Action like restructuring, removing redundant code or any improvement / implementation of best practices can be handled in the refactoring phase. Try to keep the refactoring phase as small as possible so that we can revert back at any instance.

One thing to make very sure is to re-run all test cases after a successful code refactor to confirm that the refactoring has not broken any previous implementation. This will ensure that functionality is not modified and continues to deliver the expected behaviour**.**

Benefits of Test Driven Development

**Functionality Discovery : **Developers aremore focused on understanding the project specification rather limited to the development. Since test cases are written first, developers spend more time in understanding the expectation.

**Modular Architecture : **Test Driven Development helps developers to design a modular architecture, write clean code with extensibility. Developers are more focused on small classes which provide clean interfaces.

Code Documentation : Test case description acts as a code documentation. One should write a clear description on what the test case is doing and what is the expected behaviour.

Identify Issues : With Test Driven Development developers can identify error in early stages which reduces the debugging time.

Improved Quality : Code quality is improved with reduction in bugs. Error cases are defined in early stages and developed in later phases.

**Developer Confidence : **Increases confidence in developer for rework or architecture design change which improves the productivity.

**Faster Feedback : **Developer receives faster feedback on the functionality based on the test cases designed. One can rework on test cases till the desired output is received.

**Satisfied Customer **: Since there is a loop between understanding the requirement, defining test cases and writing a production code, the program gets aligned to the business requirements.

One Step Ahead

Test Driven Development looks to be a time consuming methodology, however once the developer is comfortable it makes life easy. It’s a perfect suit for automation testing and can be quantified based on the test coverage.

Test coverage is a mechanism to determine how much code is covered by the test cases, this helps to monitor which areas are being tested. For this exploration we will use SonarQube for inspecting code quality and static code analysis.

Documentation

SonarQube ® is an automatic code review tool to detect bugs, vulnerabilities, and code smells in your code. It can…

docs.sonarqube.org

SonarQube offers different types of reports like Code Duplication, Bugs Listing, Code Smells, Test Coverage etc. Below is the sample screen from our array sort example.

Image for post

Conclusion

Test Driven Development helps to deliver modular and extensible code. Developers are forced to design smaller classes which provides a clean interface. The fact that Test Driven Development is not only validating the code but helps in driving the design of the program. An organisation becomes more efficient in making new changes to the existing program without a fear of breaking the overall system.

That’s it!!

#software-development #developer #coding-style #methodology #test-driven-development

Test-Driven Development — Build Completed Successfully, Let’s Deploy !!
1.65 GEEK