Things a developer should know to practice TDD

TDD – Test Driver Development is to force developers to think before writing their code. And it is not so easy for a developer who never tried TDD before. At the beginning of practicing TDD, I found it difficult to understand how TDD helps to design my production code. Sometimes I felt that writing tests are just a waste of time and stopping me to finish my tasks quickly. Anyway, I know it’s not just only me, many other developers feel the same way while starting TDD. Some feel that writing Unit Tests is the better approach than TDD

Here, I am pointing out a few things that may help any software developer to dive deep inside TDD world

TDD Lifecycle

RED -> GREEN -> REFACTOR (optional) … -> RED -> GREEN -> REFACTOR (required)…

Three rules of TDD from Uncle bob 

1. You are not allowed to write any production code unless it is to make a failing unit test pass. 2. You are not allowed to write any more of a unit test than is sufficient to fail; and compilation failures are failures. 3. You are not allowed to write any more production code than is sufficient to pass the one failing unit test.

Red

This is where we start. On this step, we write test. This is the time we think how you want to use your system

  • Code is not compiling or
  • Test is failing

Green

  • Code compiles and
  • All tests pass

On this step, we start writing production codes. We may need to do some fixes on our tests (because of wrong perceptions or typos) to pass tests

Refactor

This is where the TDD gets complicated and also get its beauty. This is the part where you may apply your learnings about Object Oriented design, design patterns, and best practices.

  • This is not a mandatory step on every round
  • Wait until getting something to refactor
  • No tests should fail at the end of this step
  • take small steps towards your desired design

That’s it for today and this is the end of first part. I will continue writing about those in coming posts. Please stay with me

I would like to hear your thoughts