TDD (Test Driven Development) — an Introduction

Lalitha
3 min readOct 23, 2020

As a newbie to testing , was trying to correlate testing concepts with real world examples.

As a homemaker best example I can correlate is when I bought Instant pot for the first time, was trying to test what works best for PIP type of cooking. Here what I meant for PIP is not standard package manager of python, I meant pot in pot. So I started exploring will all the pots I already had and finally decided what works best.

Photo by Robert Wiedemann on Unsplash

In similar way, TDD ( Test Driven Development ) is a software development practice / approach, where we write tests first and then we feature, i.e., we use these tests to drive the design and development of a software application.

What is TDD… ?

TDD encourages to write tests that would check the functionality of our code before writing the actual code. When we are satisfied with our tests, we will begin to write the actual code.

As we can catch a bug before production, this type of approach can reduce the cost as finding bug in development is much cheaper than finding a bug in production.

In essence we follow three simple steps repeatedly:

Now lets see the difference between traditional software process and TDD.

Traditional Model -

In traditional software process, we start with designing after collecting requirements. Then we move to implementation phase followed by testing. So if we have any bugs in first phase as we are not going to test them, we think everything is perfect and move on to next phase. But this kind of approach will not be cost effective.

How is TDD different from traditional model -

Test Driven Development is a programming technique , in which we write test first and drive code from it. Here we focus on how functionality is implemented. These test cases are written in a programming language.

Steps of TDD -

It also helps to improve your design and protect against future mistakes. TDD can be applied to any language and IDE.

RED, GREEN, REFACTOR -

Red , green , refactor is the approach that was used by developers to perform test driven development. There are three phases red, green and refactor.

Red is the phase where we write a test , this test is only passed when expectations are met. Writing a failing test is what we do in this step.

Green phase is where we write code for implementation to make the test pass.

Refactoring stage is where we refactor. In this stage all the duplication created in merely getting the test to work is eliminated. We may clean up the code if necessary.

Test Driven Development Cycle -

1. Add a test

2. Run all tests and see if the new test fails

3. Write the code

4. Run tests

5. Refactor code

Summary

As a summary, test driven development is performed by developers as they get their requirements. Depending on the requirements tests are written first and then these tests drive the code.

Thanks for reading…!!

Originally published at https://www.numpyninja.com on October 23, 2020.

--

--