Test-driven development (TDD) is an approach to software development in which software tests are written before their corresponding functions.
Developers write enough code to pass each test, then both the test and code are refined before moving onto a new test and then a new feature.
Test-driven development essentially forces developers to slow down, validate and refine their code in shorter feedback cycles. While not required, DevOps teams encourage coders, from beginners to seasoned professionals, to use TDD across a wide range of programming languages. For example, Java, Python, and so on, application programming interfaces (APIs) and program applications.
Programming in this style strengthens the relationship between coding, testing (in the form of automated unit-level tests) and code design. While test-driven development might increase upfront development time, it has been demonstrated to improve code functionality and dexterity and save time overall.
By immediately identifying and addressing any errors, developers that use TDD can prevent small issues from becoming larger problems. Test-driven development forces developers to both validate and refine their code as they go, thus streamlining final quality checks and corrections.
Alternative testing frameworks include writing the production code before writing all the automated tests or writing the entire test suite before writing the production code. These methods, while not necessarily ineffective, have been shown to increase necessary debugging times, especially with larger and more complex projects.
While test-driven development is commonly used for the creation of new production code, it is also often applied to improve debugging legacy code developed with older or other techniques.
Test-driven development reverses the traditional development process by putting testing before development. As an iterative approach, test-driven development improves code quality and readability by promoting testable workflows that result in high-quality code at the unit level. When developers implement unit testing, they focus on a small portion of logic, such as an individual algorithm. Writing code specifically to make tests pass not only results in cleaner, more durable code but also helps improve documentation.
Industry newsletter
Stay up to date on the most important—and intriguing—industry trends on AI, automation, data and beyond with the Think newsletter. See the IBM Privacy Statement.
Your subscription will be delivered in English. You will find an unsubscribe link in every newsletter. You can manage your subscriptions or unsubscribe here. Refer to our IBM Privacy Statement for more information.
There are two main levels of test-driven development.
For acceptance TDD—sometimes called Behavior-Driven Development (BDD)—programmers write a single acceptance test and then enough new code to pass. Acceptance tests are sometimes referred to as customer tests or customer acceptance tests.
They can generally be understood as test cases required for minimum functionality as outlined by product stakeholders. ATDD strives to identify detailed, executable requirements. Acceptance tests can be carried out using various testing tools, such as Fitnesse or RSpec.
Sometimes referred to as simply TDD, developer TDD requires coders to write single tests to evaluate their own solution to an ATDD test. Developer TDD uses test automation tools, such as JUnit or VBUnit.
When employing a test-driven development strategy, coders first write tests to check each individual element or function of a piece of software before writing enough code to pass that individual test. Once completed, the software is tested again, and if it passes, the code is refined (a process known as refactoring) to include only essential elements. Developers then repeat this process for each subsequent software function.
The test-driven development process is broken down into five individual steps:
Put simply, the test-driven development process follows a repeatable loop, referred to as the red-green-refactor cycle. The steps of the cycle are:
While the specific origins of test-driven development are unknown, the concept of writing tests first and production code second was not a common practice until the mid-1990s. Before that, testing frameworks separated developers from testing their own codebases. However, as software engineering evolved, DevOps teams demanded faster and more flexible methodologies to meet stakeholder demands, especially when dealing with rapidly shifting stakeholder requirements.
Test-driven development evolved from and alongside various novel testing frameworks and has been adopted as a modular component into various other frameworks, as well. Most notably, TDD is included in the concept of Extreme Programming (XP), an agile software development framework developed to improve both software quality and the quality of life for developers.
Software engineer Kent Beck, a major figure in the agile community and the creator of Extreme Programming, is credited with “rediscovering” test-driven development. In Beck’s own words:
“The original description of TDD was in an ancient book about programming. It said that you take the input tape, manually type in the output tape you expect, then program until the actual output tape matches the expected output. After I’d written the first xUnit framework in Smalltalk, I remembered reading this and tried it out. That was the origin of TDD for me. When describing TDD to older programmers, I often hear, “Of course. How else could you program?” Therefore, I refer to my role as “rediscovering” TDD.”
Notable dates in the evolution of test-driven development include:
As a component of Extreme Programming, test-driven development has been found to be beneficial for not only creating better code, but also better coders themselves. TDD can allow coders to gain better insight into their projects and help drive program design. By centering the test case before each feature is implemented, developers must visualize how a function will be used by a client or user. This approach positions the product interface before the implementation and helps developers create more user-centric applications.
Some additional benefits of test-driven development include:
Although there are many valuable benefits to using test-driven development (TDD), it is not without its challenges. While the severity of these challenges can be project-dependent or mitigated with various other techniques, some of the disadvantages of TDD include:
Helps simplify complex hybrid environments with unified infrastructure and security management.
Build, deploy and manage secure, cloud-native apps across devices and environments.
Set a new standard for cloud and hybrid operations.