What is TDD & its benefits

What is TDD & its benefits

Helpful approach for any developer

ยท

5 min read

Introduction

Have you ever faced a situation where while you fix one bug, it opens a new bug unknowingly? Here comes the rescuer TDD. TDD is an approach to follow while we write code.

Think about this situation, If someone offers you an empty cup of coffee, can you drink it? Cup is empty, So you can't drink right? I know this is a stupid question ๐Ÿ˜‚ But you can pretend to be drinking an empty cup right? ๐Ÿ˜‰ So This is how you start your TDD approach by testing an empty code. Oh! I forgot to tell the expansion of TDD, It is Test Driven Development.

It starts from failing by testing an empty code. your development has to be driven by the test cases. So, based on your testing/test case, you need to develop your code. Test before you Develop, Thats the basic formula of TDD

Disclaimer: This article follows java code, You can be any developer like typescript, dot net, python...etc. This approach is the same for all.

How to do that?

I'm going to follow TDD approach for a functionality (adding two values). MyClass is a java class that has no methods in it. So As I promised I'm going to drink a empty cup of coffee (I mean pretend to ๐Ÿ˜‚) By writing a testcase for a method called addition() which is not being even created in MyClass ๐Ÿ˜‚๐Ÿ˜‚ Isn't it funny? Lets see step by step

Step 1: Write test case


MyClass myclass = new MyClass();

@Test
public void additionTest() {
 int result = myclass.addition();
}

Here, there will be a compilation error in myclass.addition() as addition() method is not yet created in MyClass

If you run the above code, obviously, you can't run it as it throws a compilation problem. (So successfully you've created a failing testcase before writing a single actual code). Now you are going to fix that by creating addition method in MyClass. then you can run test case again. Now it will be succeeded

Step 2: Add parameters

@Test
public void additionTest() {
 int result = myclass.addition(10,12);
}

Now I'm going to pass two parameters in test case, You can see a compilation problem again because in addition method of MyClass doesn't have declared parameters. so declare a parameter in addition method of MyClass, then you can run test case again, Now it will get passed.

Now your code will be something like this in MyClass,

public int addition(int a, int b) {
  return 0;
}

Step 3: Assert the result

@Test
public void additionTest() {
 int result = myclass.addition(10,12);
 Assert.assertEquals(result, 22);
}

Here, I'm checking the result of the testcase. now, If you run your test case, it will get fail because addition method always returns 0. Because we haven't done actual implementation so far. Fix that by adding actual implementation. Your implementation will be like below

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

Now, run your test case, it will get pass ๐Ÿ˜Š This is how you should progress, First Write a failing test case then fix it

Is this approach annoying by consuming a lot of your time for development? ๐Ÿ™„ If you feel so, then you are in the right mindset. This is one of the time taking processes for the initial development, but it will save a lot of your time while you fix/change anything in your actual functionality later point in time.

For example, there is a new implementation to pass 3 parameters in addition method. By that time, you will have all the test cases handy. Now go and change the implementation by passing an additional parameter. By default, all of your tests will have compilation problems as they would have 2 parameters. Fix the test case, run it again. see how it behaves. Since the test case will be covered all of your functionality even if there is any problem inside it, your test case will get fail by default.

Real Benefits:

(My benefits)

1) This will ensure 100% code coverage including all the blocks, nested methods, etc.

2) Your test case will be intelligent enough to identify the new issues which arise while you fix another issue. Because test case will get fail, you will be notified ๐Ÿ™‚

3) You will start thinking as a tester while you develop ๐Ÿ˜‚๐Ÿ˜…, So tester will have hard time finding functionality-related issues ๐Ÿ˜Ž๐Ÿ˜Ž

4) If a new change comes, you can easily adopt and test quickly by running test cases.

5) Dependent Method Scenario: If your method is using any common method inside it.

Your method name is addition(). common method name is checkZero(). If you code like this

public void addition(int a, int b) {
  if(checkZero(a,b)) {
    return a+b;
  } else {
    throw new Exception("value cannot be zero")
  }
}

public boolean checkZero(int a, int b) {
 // some logic to check zero & return boolean
}

Consider checkZero is being used all the other methods as well such as subtraction(), division()..etc

Let's say there is a new requirement to do some changes in checkZero, Now, there is a possibility of breaking other methods(addition,subraction,division..etc) which are all dependent on the common method checkZero right? Because other methods might get upset.

Now your test case will help you to identify the problems if there is any breaking in existing functionality Isn't it awesome? ๐Ÿ˜ Cool ๐Ÿ˜Ž

Here, I'm concluding this article as you might started thinking and understanding the real benefits of using TDD. I hope this article triggered your mind to follow TDD approach and to know how it will be beneficial for a developer. Have a great day ๐Ÿ˜‰๐Ÿ˜Š See you in next article ๐Ÿ•บ

ย