Test-Driven Development (Part 2): How to do TDD for Beginners!

Sailee Renapurkar
5 min readMay 9, 2020

--

In the previous blog I have covered the ‘Why’ of TDD. Now let’s talk about the ‘How’ of TDD. To help explain this I will take the example of a Calculator. Let’s talk about how one can start TDDing. But before we do, it’s important to know that each language provides its own testing frameworks and it is important to know a little bit about which framework you’re dealing with. I will be using Java for the purposes of this post though similar concepts exist in most languages you’ll be developing in.

Some of the important Practices when following TDD:

  • It’s a good practice to keep test and source code separately.

e.g “src/main/java” is for the code and

“src/test/java” is for the tests

This ensures that the test code does not get packaged and sent to production.

  • If the Class Name is Calculator then the Test Class needs to represent the class name and would like CalculatorTest i.e simply append the name test to the class name. If you’re using a language like Python, the standard is to prefix the word “test_” to your file name (making it test_calculator.py)
  • If the Function Name is addTwoNumbers(int number1,int number2) then the test names for the function should be according to types of scenarios we intend to test.

e.g testShouldBeAbleToAddTwoNumbers()

Speaking more precisely, the test name should explain the intent of the test

Let’s start with the example of writing code for a simple calculator.

I am going to use JUnit 5.4 framework for this and add the jar as an external library in the project. One can add the dependency in build.gradle file if you are using gradle for dependency resolution or in pom.xml if using maven.

The first requirement is to have a function for adding two integers.Remember, its always Red, Green, Refactor

I created a test class for that, the CalculatorTest.

`@Test` annotation tells JUnit to run shouldAddTwoIntegers() as a test and report if it passes or fails. So as long as assertions pass, the tests are considered to be passed. Our test code would be following the Given When Then pattern.

Now when I run this test class, it shows the test fails.

I will add the code that makes the test green a simple add function.

Now when I run the test, the test passes and the tests are green.

It’s time to do refactoring if any is required. We get a new requirement that the system should work for double values.

I will refactor the test code by adding a new requirement of adding double values.

Again it’s red.

Given that we have code only for int, we need to refactor the code to make it work for double also.

Make it green. The cycle continues until we see all the acceptance criteria for a given requirement is met in the code.

Yippee! It’s green again and I am sure about the method now.

Time to add some more scenarios let’s check if the function adds when negative values and gives the right results.

Both the tests are green.

We see for every test we have to create an object initialization. Why not do it once for all the tests in this given context. In this case, we can leverage on a feature Junit provides by adding an annotation called `@BeforeAll`, this creates an object that is created before any test runs and that is used by each method.

In a similar way we can do other operations. Let’s take the example of dividing two integers.

Let’s first write a basic test for it.

Now I will add only the code to make it green.

And the test is green now. That means we have met our requirement.

Let’s add a test to check division by zero returns an exception.

Yippee! The test is green.

I have tried to explain some very basics of TDD with a simple short example. The real-world examples are going to be a lot more complex and the principle followed though are still the same. The only way to understand this is by practising, the more one practices this gets the simpler it becomes and we start to understand why it is important and how it is useful for us as developers.

There is a lot more to know about quality and tests, I will continue to share my journey as and when I am gaining proficiency on some of these tools and techniques as I spend more time in the industry for now signing off with a hope that it helps people who are new to TDD.

--

--

Sailee Renapurkar
Sailee Renapurkar

Written by Sailee Renapurkar

Solution Consultant At Sahaj Software Solutions Pvt Ltd.

No responses yet