Test Driven Development (TDD) : Introduction

Hicham BEN KACHOUD
2 min readJan 19, 2020

--

Two years ago i heard that there is a technical which reduce potential bugs in production code, but i was sceptical.

The idea that “ I have to write code to check if the code i wrote was right” was meaningless.

What is TDD ?

Test Driven Developement, or TDD, is a software development technique. The very idea is that, we write unit tests before writing the production code.

The fact of writing test against no code is more important to write cleaner and more purposeful code. It is opposite of traditional coding habit, where we create code first, then manually run the unit to make sure it does what we intended manually.

A programmer taking a TDD approach refuses to write a new feature until there is first a test that fails because that feature isn’t present. In fact, they refuse to add even a single line of code until a test exists for it. Once the test is in place they then do the work required to ensure that the test suite now passes.

TDD Steps :

TDD consists of many steps, which are:

  • Write a test
  • Run it, and see it fail
  • Write the production code
  • Run test again and see it pass
  • Refactoring code
  • Repeat step 1

TDD process :

Every time i hear the word TDD, i think about “Red, Green, Refactor” cycle, which work in a small to get the test code to pass.

Red : it represents the unit test that fail at first runnig.

Green : it represents the production code to make the test pass.

Refactor : it represents the way of cleaning code and keep it simple.

TDD benifits

There are several benefits to following a TDD approach.

  • It helps to minimise the time needed to refactor your code.
  • It makes adding new features much easy, and refactor your code wihtout worry.
  • It helps to maintain your old code.
  • It helps get faster feedback.
  • It gives the programmer confidence to change the large feature of an application easily.

Conclusion

Of course, this was a short introduction of TDD method. I hope you gained a small introduction to what the TDD process is like. Testing does take a bit of practice and investment in time. I am working on an example of TDD implementation that will be soon posted.

--

--

Hicham BEN KACHOUD
Hicham BEN KACHOUD

Responses (1)