Blog

Home / Blog

A re-introduction to Test Driven Development (TDD)

  • 09 Nov 2016
  • No Comments

Test Driven Development (TDD) is an age old programming discipline that is not very commonly followed, reason being unawareness for some developers and negligence for others.

Let us dive into the details of what TDD is and how it helps.

Robert C. Martin broke TDD into three basic laws to make it simple, and they are:

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 the tests; 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.

These three laws ensure that you have only enough code to be able to cover the tests you have written. You will end up having code that is proven by your tests, only to cover the requirements defined by the tests. You will find that your test coverage will be nearing 100%, provided you followed the three rules.

If you do not follow the rules, you will find that writing tests for your code would be boring and dull. You would have the feeling that you (most probably) have your code working, and would have gone through a few rounds of manual testing. You would find that some of your code will not be testable at all, full of functions that do more than one thing, violating multiple SOLID principles.

Unit tests are supposed to be as trust worthy as a parachute that you would want to equip yourself with while jumping out of an air-plane. TDD promises you that same thing. You cannot write non-testable code as all the production code that you write is to make your last failing test pass, hence your code is decoupled as it should be.

What you gain when you follow these laws?

a) Decoupled production code
b) Unit tests with almost 100% code coverage
c) Less time debugging because all your tests passed and everything worked just a few minutes ago

For those who think it makes you slower… you might want to rethink.

People should be educated about TDD. Managers, architects, leads and everyone should promote TDD in their teams and build quality software. Startups these days which have just one product that helps them earn their bread and butter should consider TDD as their daily discipline which will ensure that they are strong from the tech side!

For more detailed explanation and you may go ahead and watch some of these: Clean Code: Fundamentals – Episode 6 TDD

Published by

Kamran Ahmed Ansari

Kamran is a Software Engineer at Indus OS. With close to 3 years of experience in software development he is very well versed with all things tech. A math & a gadget lover at heart he spends his free time sharpening his coding skills.

Leave a Reply

Your email address will not be published. Required fields are marked *