Embracing Test Driven Development

It’s been a year now since Test Driven Development has been first introduced to me.
I have been coding for several years prior to this yet I didn’t bother to dig deeper and practiced it.

Test-Driven Development is an approach which uses specifications written as test cases to shape the way on how you will implement a feature. A process that can be summarized as

“Writing TESTS FIRST before CODING“.

Test Driven Development

Yup. You’ve heard it right. I was even overwhelmed by its idea during my first few months.

TDD requires you to define what you expect a feature will do represented by a test case at a level of precision not generally covered by any specification provided by clients.

This approach is quite challenging. Imagine breaking down and write the client specifications into unit tests first without writing any actual code or implementation.

However, with these unit tests, you will have an abstract high level overview on how you will implement your solutions with actual validations.


public function test_some_feature() {
    // My Library feature
    $library = new Animal_Library();
    $sound = $library->sound('dog');

    // verify expected library feature result
    $this->assertEquals($sound, 'woof');
}

The code above is a simple test case of a feature. This verifies the event of using the method ‘sound’ of ‘Animal_Library’ with ‘dog’ as the parameter will always return ‘woof’ whenever this method is invoked in the application. Very convenient right?

If you have been accustomed in writing your code right away, this approach can somehow be frustrating since writing unit tests can sometimes consume a lot time.

As it had become a daily practice, I have slowly appreciated the benefits and get accustomed to this approach.

With Test Driven Development, I can;

  • Easily verify that the code written full-fills the client specifications using the Unit Tests created.
  • Save time by reusing the function already written or invoked in your test cases.
  • Ensure any code refactoring and optimization doesn’t have side effects which results to a cleaner and better code.
  • Drastically drop the chances of your code to fail since you have clearly planned your solution  instead of coding directly without defining your course of action.