Test-Driven Development (TDD)

Abhishek Srivastava
9 min readMar 5


The evolution of Agile development has introduced many pragmatic practices for delivering quality software at high speed. Test-Driven Development (TDD) is one such practice that is now recognised as an efficient approach that drives positive results.

Test Driven Development is the process in which test cases are written before the code that validates those cases. It depends on repetition of a very short development cycle. Test driven Development is a technique in which automated Unit test are used to drive the design and free decoupling of dependencies.

Test Driven Development (TDD) is software development approach in which test cases are developed to specify and validate what the code will do. In simple terms, test cases for each functionality are created and tested first and if the test fails then the new code is written in order to pass the test and making code simple and bug-free.

TDD usually follows the “Red-Green-Refactor” cycle:

  1. Add a test to the test suite
  2. (Red) Run all the tests to ensure the new test fails
  3. (Green) Write just enough code to get that single test to pass
  4. Run all tests
  5. (Refactor) Improve the initial code while keeping the tests green
  6. Repeat

This process sounds slow, and it often can be in the short-term, but it does improve the quality of the software project in the long-run. Having adequate test coverage acts as a safeguard so you don’t accidentally change the functionality. It’s much better to catch a bug locally from your test suite than by a customer in production.

What is TDD?

The basic concept of TDD is that all production code is written in response to a test case. Robert C. Martin, who is known as Uncle Bob, describes these Three Laws of TDD:

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

Three phases of Test Driven Development

  1. Create precise tests: Developers need to create precise unit tests to verify the functionality of specific features. They must ensure that the test compiles so that it can execute. In most cases, the test is bound to fail. This is a meaningful failure as developers are creating compact tests based on their assumptions of how the feature will behave.
  2. Correcting the Code: Once a test fails, developers need to make the minimal changes required to correct the code so that it can run successfully when re-executed.
  3. Refactor the Code: Once the test runs successfully, check for redundancy or any possible code optimisations to enhance overall performance. Ensure that refactoring does not affect the external behaviour of the program.

The image below represents a high-level TDD approach towards development:


  • Unit test provides constant feedback about the functions.
  • Quality of design increases which further helps in proper maintenance.
  • Test driven development act as a safety net against the bugs.
  • TDD ensures that your application actually meets requirements defined for it.
  • TDD have very short development lifecycle.
  • Reduce costs
  • Make refactoring and rewriting easier and faster (“make it work” with red and green stages, then refactor “to make it right”)
  • Streamline project onboarding
  • Prevent bugs and coupling
  • Improve overall team collaboration
  • Increase confidence that the code works as expected
  • Improve code patterns
  • Eliminate fear of change

How TDD works?

TDD starts with designing and developing tests for every small functionality of an application. TDD framework instructs developers to write new code only if an automated test has failed. This avoids duplication of code.

The simple concept of TDD is to write and correct the failed tests before writing new code (before development). This helps to avoid duplication of code as we write a small amount of code at a time in order to pass tests. (Tests are nothing but requirement conditions that we need to test to fulfill them).

Test-Driven development is a process of developing and running automated test before actual development of the application. Hence, TDD sometimes also called as Test First Development.

Advantages of TDD

Test-driven development can produce applications of high quality in less time than is possible with older methods. Proper implementation of TDD requires the developers and testers to accurately anticipate how the application and its features will be used in the real world.

TDD creates a regression-test suite as a side effect that can minimize human manual testing, while finding problems earlier, leading to quicker fixes. The methodical nature of TDD ensures much higher coverage and first-time quality than classic phased code > test > fix > retest cycles. Because tests are conducted from the very beginning of the design cycle, time and money spent in debugging at later stages is minimized.

Disadvantages of TDD

TDD requires considerable skill to be successful, especially at the unit level. Many legacy systems are simply not created with unit testing in mind, making isolation of components in order to test impossible.

Further, many programmers lack the skills to isolate and create clean code. Everyone on the team needs to create and maintain the unit tests, or else they will quickly get out of date. And an organisation looking at TDD will need to invest time — to slow down a bit now in order to go faster later.

Finally, as with any method, the final results of TDD are only as good as the tests that have been used, the thoroughness with which they have been done and the extent to which they mimic conditions encountered by users of the final product.

How TDD fits into Agile development?

Agile development demands regular feedback to develop the expected product. In simple terms, one can also term Agile development as Feedback Driven Development.

There’s a high probability that project requirements may change during the development sprint cycle. To deal with this and to build products aligned with the client’s changing requirements, teams need constant feedback to avoid dishing out unusable software. TDD is built to offer such feedback early on.

TDD’s test-first approach also helps mitigate critical bottlenecks that obstruct the quality and delivery of software. Based on the constant feedback, bug fixes, and addition of new features, the system evolves to ensure that everything works as intended. TDD enhances collaboration between team members from both the development and QA teams as well as with the client. Additionally, as the tests are created beforehand, teams don’t need to spend time recreating extensive test scripts.

Frameworks for Test Driven Development

Based on unique programming languages, there are multiple frameworks that support test driven development. Listed below are a few popular ones.

  1. csUnit and NUnit — Both are open source unit testing frameworks for .NET projects.
  2. PyUnit and DocTest: Popular Unit testing framework for Python.
  3. Junit: Widely used unit testing tool for Java
  4. TestNG: Another popular Java testing framework. This framework overcomes the limitations of Junit.
  5. Rspec: A testing framework for Ruby projects

The process of delivering quality products requires not just debugging but also demands optimization in the development process. When incorporated correctly, the TDD approach provides numerous benefits, particularly in terms of bringing cost-efficiency in the long run and delivering true value to businesses.

TDD cycle defines

  1. Write a test
  2. Make it run.
  3. Change the code to make it right i.e. Refactor.
  4. Repeat process.

Some clarifications about TDD:

  • TDD approach is neither about “Testing” nor about “Design”.
  • TDD does not mean “write some of the tests, then build a system that passes the tests.
  • TDD does not mean “do lots of Testing.”

TDD Vs. Traditional Testing

Below is the main difference between Test driven development and traditional testing:

TDD approach is primarily a specification technique. It ensures that your source code is thoroughly tested at confirmatory level.

  • With traditional testing, a successful test finds one or more defects. It is same as TDD. When a test fails, you have made progress because you know that you need to resolve the problem.
  • TDD ensures that your system actually meets requirements defined for it. It helps to build your confidence about your system.
  • In TDD more focus is on production code that verifies whether testing will work properly. In traditional testing, more focus is on test case design. Whether the test will show the proper/improper execution of the application in order to fulfill requirements.
  • In TDD, you achieve 100% coverage test. Every single line of code is tested, unlike traditional testing.
  • The combination of both traditional testing and TDD leads to the importance of testing the system rather than perfection of the system.
  • In Agile Modeling (AM), you should “test with a purpose”. You should know why you are testing something and what level its need to be tested.

Unit testing as part of TDD

If your team is hesitant to skip traditional unit tests, remember: TDD drives the code development, and every line of code has an associated test case, so unit testing is integrated into the practice. Unit testing is repeatedly done on the code until each unit functions per the requirements, eliminating the need for you to write more unit test cases.

At IBM, teams found that the built-in unit testing produces better code. One team recently worked on a project where a small portion of the team used TDD while the rest wrote unit tests after the code. When the code was complete, the developers that wrote unit tests were surprised to see that the TDD coders were done and had more solid code.

Unlike unit testing that focuses only on testing the functions, classes, and procedures, TDD drives the complete development of the application. Therefore, you can also write functional and acceptance tests first.

To gain the full benefits of unit testing and TDD, automate the tests by using automated unit test tools. Automating your tests is essential for continuous integration and is the first step in creating an automated continuous delivery pipeline.

Scaling TDD via Agile Model Driven Development (AMDD)

TDD is very good at detailed specification and validation. It fails at thinking through bigger issues such as overall design, use of the system, or UI. AMDD addresses the Agile scaling issues that TDD does not.

Thus AMDD used for bigger issues.

The lifecycle of AMDD

In Model-driven Development (MDD), extensive models are created before the source code is written. Which in turn have an agile approach?

In above figure, each box represents a development activity.

Envisioning is one of the TDD process of predicting/imagining tests which will be performed during the first week of the project. The main goal of envisioning is to identify the scope of the system and architecture of the system. High-level requirements and architecture modeling is done for successful envisioning.

It is the process where not a detailed specification of software/system is done but exploring the requirements of software/system which defines the overall strategy of the project.

Why is it important to see a test fail?

It may seem odd, but it’s just as important to see a test fail as it is to see a test pass in TDD.

Put simply, a failing test:

  1. Validates that the new test is meaningful and unique, helping to ensure that the implemented code is not only useful but necessary as well.
  2. Provides an end goal, something for you to aim for. This focuses your thinking so that you write just enough code to meet that goal.

In next part, I will discuss about TDD with Android.

Thanks for reading…



Abhishek Srivastava

Senior Software Engineer | Android | Java | Kotlin |Xamarin Native Android |Flutter |Go

Recommended from Medium


See more recommendations