Test Driven Development - As a Beginner

 


My Journey with Test-Driven Development


Many years ago, when I started working on a new project, my Architect & the head of the Project wanted us to follow Test Driven Development (TDD) approach. That was the first time I heard about this concept. As with any other developer even I was in the mindset that writing a test first is not going to work and is very time-consuming. But, I wanted to give it a try. I started by writing a test for a simple method that I needed to implement in my code.

Here's an example of the test that I started playing with TDD:


public void testAddition() {
    Calculator calculator = new Calculator();
    int result = calculator.add(2, 3);
    assertEquals(5, result);
}

This test checks that the add method of the Calculator class correctly adds two numbers together. The assertEquals method checks that the result is equal to the expected value, which is 5 in this case. Next, I ran the test and of course, it failed because I hadn't written any code yet. 

This looks very simple and I was questioning myself multiple times that what is the point in writing a test for the code that I haven't written anything yet! But I continued with the prescribed formula of TDD.

Step 1: Write a failing test

Step 2: Make the test pass

So I implemented the add method in the Calculator class to make the test pass. Here's the code for the add method: 


public class Calculator {
    public int addition(int a, int b) {
        return a + b;
    }
}
Once I implemented the method, I ran the test again, and this time, it passed. As defined in TDD, Step 3: Refactor the code.     I then refactored the code to make it more efficient and easier to read. In this case, I refactored the method name to add instead of addition. In a larger project, there may be more opportunities for refactoring and optimization.

Here's an example of another test that I wrote to test the multiply method of the Calculator class:


@Test 
public void testMultiplication() {
    Calculator calculator = new Calculator();
    int result = calculator.multiply(2, 3);
    assertEquals(
6, result);
}


Again, I ran the test and saw that it failed. I then implemented the multiply method in the Calculator class and ran the test again. This time, it passed.

Here's the code for the multiply method:

public class Calculator {

    public int add(int a, int b) {
        return a + b;
    }

    public
int multiply(int a, int b) {
        return a * b;
    }
}

I continued to write tests for each method that I needed to implement and followed the same process of writing the minimum amount of code necessary to make the test pass, refactoring the code, and running the test again.

By practicing TDD, I was able to catch bugs early in the development process, The design evolved and eventually, I followed the right design principles. I was confident that my code was doing what it was supposed to do. I also found that writing the tests first forced her to think about how I wanted my code to work before I started writing it, leading to cleaner, more efficient code that was easier to maintain and update.

In the end, I realized that TDD wasn't just a development process, it was a way of thinking about and approaching software development. It helped me write better code, catch bugs early, and deliver high-quality code on time and within budget.

This I have made is as a practice and I still follow the TDD approach whenever I get a chance to code. I try to mentor and coach my team members as well and embrace the team to follow good practices. 

Comments

Popular posts from this blog

Export data from MySQL to CSV file