Test-Driven Development

As computer programs become increasingly complex, the agile methods of test-driven development (TDD) are also becoming ever more popular. For good reason: TDD helps programmers ensure the design of their software program is well thought-out before they proceed to write the functional code. Not only does this increase the quality of the software considerably, but also reduces the maintenance costs.

Test-driven development is used in extreme programming, for example, which is characterized by ongoing reviews, tests, design, and redesign. TDD likewise follows a development cycle and its sequence needs to be maintained for effective implementation.

What is test-driven development?

There have been a variety of test methods that manage software quality for quite some time. At the start of software development, independent testers in quality management would examine computer programs in terms of their functionality. Back then, the actual development of software and the testing procedures were still considered to be separate processes. The test-first approach did not emerge until US software developer and founder of extreme programming Kent Beck published his work. His approach simply reversed previous practice: Instead of first writing the source code and then testing it, the development team began by writing the tests. The team then used the test cases to write and implement the best possible code.

Even though test-driven development may initially seem counterproductive to laypeople, it certainly has its advantages and can lead to better results. While a waterfall or V model is applied in conventional, subsequent testing, TDD processes follow a cycle. This means that test cases which often fail are defined on purpose. This is often the first step followed by writing only as much code as required to pass these tests. Subsequently, the components are refactored. While maintaining function, the source code is expanded or restructured if necessary.

How exactly does test-driven development work?

Test-driven development is based on the results of test cases you define. The cyclical approach ensures that the code is only applied to the functioning system once all requirements on the software are met. This means that you refactor and retest code components as often as necessary until the test is no longer failed. This method allows you to gradually augment the software with new functions, since you write a new piece of source code after each test is passed. For this reason, TDD is also considered an incremental model of software development.

Individual test cases typically go through the cycle for no longer than a few seconds or minutes. This way, the results can quickly be seen in the functioning code. For efficient iteration, you’ll need a TDD tool and framework. Usually, developers use a tool for build automation like CruiseControl or Jenkins. These enable the continuous and faultless integration of components in the source code. JUnit, Maven, and Ant are also popular in Java development. In general, the tests are always written in the same language as the functioning code. For PHP, you can use tools like Ceedling or CMock, for example.

But how is the test method applied? The cycle which programmers follow in test-driven development is also known as the red, green, refactor cycle. This describes the individual phases that you go through for maximum efficiency:

  1. Red phase: In this phase, the user perspective is considered which means that code needs to be simple. In other words, you write a test that contains components that have not yet been implemented. Therefore, you need to make a decision on the elements that are essential for a piece of code to work.
  2. Green phase: Let’s assume the test fails and is marked red. You now step into the role of a programmer who tries to find a simple solution. Most importantly, you only write as much code as necessary. You then integrate it into the functioning code so that the test is marked green.
  3. Refactoring: In this step, the functioning code is “tidied up” and perfected in its structure. This means that you should amend and restructure it so that it becomes simple and elegant to read from a developer’s perspective. Steps involved include removing duplicated code, for example.

Make sure that the individual activities do not overlap, i.e. don’t write tests in Phase 2 or 3 or functioning code in Phase 1 and 3. The following video shows how test-driven development works in practice:

To display this video, third-party cookies are required. You can access and change your cookie settings here.

How does TDD differ from other test methods?

Test-driven development is a design strategy that guides the development process for a software program using various tests. In contrast to downstream methods, the test cases in TDD are part of the software design from the very beginning. The tests used in TDD vary in terms of purpose and scope. The simplest test is the module test or unit test. This tests the individual components of a computer program. Integration and function tests are more complex. They are used to assess the interplay of various system parts and the overall functionality of a software program.

A few years ago, behavior-driven development (BDD) emerged from the TDD approach. In the case of BDD, a developer team initially focuses only on the desired behavior of the software, rather than the correctness of the code. The advantage here is that you do not need any technical coding skills to write test cases and can, therefore, involve stakeholders and quality managers in the development process. In general, BDD defines the best approach for writing tests, while TDD provides neat architecture.

The table below briefly summarizes the advantages and disadvantages of test-driven development:

Advantages Disadvantages
Software is at a high level and contains fewer bugs. It requires coding skills and more time to become familiarized.
System architecture and functional code are clear and well-structured. It only tests the correctness of the code and not the software’s suitability for use.
The error analysis is faster and maintenance work is reduced. It may need to be supplemented with other test methods.
It removes redundancies in code and prevents over-engineering.  

Although you can also use the various test methods individually, you can produce a higher-quality software program by combining test-driven and behavior-driven development methods. As a result, the end user will appreciate the final product all the more.

We use cookies on our website to provide you with the best possible user experience. By continuing to use our website or services, you agree to their use. More Information.
Page top