Test-Driven Development In A Nutshell

As the name suggests, TDD is a test-driven technique for delivering high-quality software rapidly and sustainably. It is an iterative approach based on the idea that a failing test should be written before any code for a feature or function is written. Test-Driven Development (TDD) is an approach to software development that relies on very short development cycles.

Understanding Test-Driven Development

Test-Driven Development has had a significant influence on software design, with a particular focus on non-functional requirements such as usability, security, and maintainability.

These small tests allow developers to design systems from scratch, adding value rapidly and increasing confidence in the system itself.

A typical Test-Driven Development cycle

TDD invariably follows the “Red-Green-Refactor” cycle, described as follows:

  • Start by adding a test unit to the test suite. This test unit should describe an aspect of the program. 
  • (Red) Run all tests, which should fail because the program lacks that feature.
  • (Green) Write just enough code in the simplest way possible to enable the test to pass. This step is sometimes called “make it pass”.
  • Run all tests once again.
  • (Refactor) Enhance the original code while keeping tests green. Ensure that a focus on simplicity is maintained.
  • Repeat and accumulate unit tests over time.

Strengths and weaknesses of TDD


  • Increased code quality resulting from the reduction in defects and errors. TDD forces developers to consider the purpose and specification of code. It also helps developers simplify code writing and avoid complex code that can harbor bugs. Lastly, TDD refactoring ensures that code changes or new code do not impact the integrity of the existing code.
  • Reduction in batch size. In other agile methodologies, developers work on a feature until completion with little input from others. TDD encourages the developer to reduce the batch size to improve project visibility, flexibility, and stakeholder collaboration. Ultimately, this also makes TDD a customer-centric agile process.
  • Encourages high developer morale. On occasion, developers become demoralized in the face of large and complex projects. TDD distills large projects into many smaller pieces that deliver quick and relatively easy wins for the developer. In addition to improving morale and confidence, the developer knows what has been completed and what work remains to be completed.


  • May take more time to implement initially. Programmers who have become accustomed to writing code that can be run immediately may find TDD slow and difficult to implement. The same can be said for programmers who have never written tests in the first place. However, any perceived slowness is an illusion at best. Although TDD may take some getting used to, it does deliver significant benefits to efficiency as development progresses.
  • Poorly maintained or abandoned test suites. A lack of test suite maintenance can lead to a TDD cycle with a prohibitively long running time. Test suites may be also be abandoned or seldom run because of poor maintenance or changes to team composition.

Test-driven development example

In the following test-driven development example, consider a user management system enabling new customers to create a username and password for future authentication.

For the username and password to be valid, certain business rules should be followed.

To analyze multiple rules would be beyond the scope of this article, so for the sake of brevity, we will describe one rule that validates the minimum length of the password.

Let’s also assume that the company is stringent on security and requires the password to be at least 12 characters.

With this rule defined, we can now create our tests and code accordingly using a language such as Java or Python.

Step 1 – Write a failing test case

The development team starts by writing the code for a failing test case. In this example, they assert that if the length of a password entered by the user is 7 characters, then they should receive an error message.

This can simply read as follows: “Length of password must be at least 12 characters.

To achieve this, the team calls a function “ValidatePassword” which takes a string argument.

The function also becomes part of a class called “ValidatorService” which is initiated in the setUp function of the test class.

Step 2 – Run the unit test

The team then runs the test but receives an error because it has not yet created the “ValidatorService” class.

The class is created and the test is run once more.

Remember that test-driven development requires that the team write just enough code so that the failing test passes.

After running the test for a second time, another error related to the test looking for the function presents itself.

The team develops a placeholder function and runs the test for the third time.

The test passes.

Step 3 – Create the code

Now it’s time to write the code for the actual functionality of the user management system.

In other words, to authenticate the length of the password and execute the test case.

The team runs a few generic test cases and each passes, but they notice as a result that there is repetition in the code.

They decide to refactor the code to maintain a focus on simplicity without impacting the viability of the test.

Since the team is using PHPUnit to write the code, they can take advantage of the “dataprovider” feature which makes the refactoring process simpler.

The team also refactors the implementation code, ensuring that after every change the test cases still pass.

At this point, more test cases can be added to validate different business rules. 

The password, for example, may require a certain number of special characters or upper case letters.

Whatever the rules that are implemented, it’s important that developers refactor the code as and when required so that problems which cause failed tests can be pinpointed with ease.

Key takeaways:

  • Test-Driven Development is an agile software development approach that favors the rapid delivery of very short development cycles.
  • Fundamental to Test-Driven Development is the “Red-Green-Refactor” test cycle. TDD argues that fail tests should be conducted before any feature or function code is written.
  • Test-Driven Development increases code quality and developer morale while reducing batch sizes. However, some project teams may experience a steep initial learning curve – particularly if they are unaccustomed to the methodology.

Read Next: Business AnalysisCompetitor Analysis, Continuous InnovationAgile MethodologyLean StartupBusiness Model InnovationProject Management.

Main Free Guides:

Scroll to Top