• Jojo Baldeo

Test-Driven Development: Avoiding the pitfalls

Developers, hold your horses. Most of us think TDD is a waste of time. Honestly, I am convinced otherwise. In this article, I will provide insights for a successful TDD implementation.

Why TDD?

TDD helps in the design. Break user stories into smaller components and develop test cases and test scripts ahead of the development phase. TDD is more a design process than a quality assurance process and is an established technique for delivering quality codes. TDD prevents over-engineering since developers focus on writing codes to pass the test cases and not include other functionalities that are not in the requirements. Most developers are guilty of adding codes they think will be used in the future but is not in the current scope.

What are pitfalls to watch for?

TDD is a very challenging task, and here are the typical individual and team pitfalls doing TDD:

  1. Selective adaptation. Not everyone on the team practices TDD.

  2. Failing to run tests before code check-in and after refactoring.

  3. Being lazy to refactor codes.

  4. Failing to perform regression testing.

How to become successful with TDD?

Uncle Bob, Robert C. Martin, the author of the Clean Code book and the founder of the Agile Manifesto, provides three laws of TDD:

  1. You must write a failing test before you write any production code.

  2. You must not write more of a test than is sufficient to fail or fail to compile.

  3. You must not write more production code than is sufficient to make the currently failing test pass.

He also describes the micro-cycle that experienced TDD follows. The Red/Green/Refactor cycle. The three cycles are:

  1. RED - Create a unit test that fails.

  2. GREEN - Write production code that makes the test pass.

  3. REFACTOR - Clean up the code

What should I do as a developer then?

  1. The first step after requirements engineering is to analyze the problem, consider all input possibilities, and determine the required output.

  2. Setup your tools, toolchain, and your IDE.

  3. Develop an application library for your project.

  4. Don't write your test codes. QA people must write test codes.

  5. Maintain version control.

A product that did not go through an extensive quality assurance process is bound to fail. In agile software development, developers release features that require testing to ensure it meets functional requirement and usability.

Online References:

Uncle Bob: The Cycles of TDD

Andrea Koutifaris: Test-Driven Development What it is and what it is not

Kent Beck: Test-Driven Development by Example

Mohamed Taman: Test-Driven Design Java

Agile Alliance: Agile Alliance Web Site