Blog

Explaining Test Driven Development (TDD) Part 2: A Practical Example

In Explaining Test Driven Development: Part 1, I explained to you what TDD was, the steps of TDD and why it is so important as a developer to use tests. Here, in Part 2, I will help you understand TDD further by mapping out a “Programming” example. In this post, we will not write code just yet, but use a theoretical calculator example to make sure you thoroughly understand how the TDD cycle works before we move on to writing code. 

First, let’s define the concept to be tested…

Explaing Test Driven Development (TDD) Part 2: A Practical Example

Let’s suppose a really important client (by the way, all clients are important!) asked us to create a calculator app and it only needs to add. Not more, not less. It’s important to understand that there are a lot of ways to define the app’s logic. Some developers like to create mockups first, others program the logic first and take care of the visual next and so on. There is no perfect way to program because each brain is different and each programmer thinks in different ways. Here, we are going to develop the logic by using TDD from the beginning. Nothing complicated, but rather something practical that will help us demonstrate the power of TDD.

Now, let’s apply the test driven development steps, from my previous post, in order to create our app’s logic: 

1. Write a test for a desired feature

Remember, TDD defines we should create tests before we write any code. As stated above, the only requirement of our calculator is to “add”. Therefore, we need to create a test for the functionality of “add” (this does not include any function logic, just a simple test). If the test is for an “add” function, we would expect, given two numbers, the function should return the correct result for the operation. The test must include all the test logic.

 

2. Run that test and watch how it fails!

This is very simple. You only need to run the test and, because we have not written any functionality yet, it will definitely fail.

 

3. Implement the feature (Just the structure, not the logic yet).

Now we are going to create the skeleton of the function (Not the logic, it does not need to make any process quite yet). We only need the function declared.

 

4. Run the test, and watch how it fails again!

Let’s run the test again. Due to the fact that the function only has the declaration, it will fail again.

 

5. Implement the logic, syntax, etc.

Now comes the moment the function has to be completed. If we are talking about an “add” function, we would expect that, given two numbers, the function must implement all the code necessary in order to return the correct result. By the end of this step, the function must work as expected.

 

6. Finally, watch the test PASS!

Finally, we should be able to run the test and watch that everything is working as the client expects. Possibly, this is the cycle’s best part, because there are no errors in the console! At this point, we have a function created with the appropriate test. We are ready to deliver the app to the client and to demonstrate that it really works.

Remember, for future enhancements, we need to repeat the entire cycle again. 

Now that you’ve seen a practical example of TDD in action, next week, in part 3, I will show you an example of TDD using NodeJS. If you would like to know more or have any questions about Test Driven Development or best practices please follow me on twitter and github, subscribe to our blog or comment below and I will be happy to answer. 

At Gorilla Logic we hire only the best software engineers. Think you have what it takes to be a Gorilla? Check out our careers section and follow us on LinkedIn! 

Ready to be Unstoppable? Partner with Gorilla Logic, and you can be.

TALK TO OUR SALES TEAM