+4 votes
150 views
in Web development by (242k points)
reopened
Test-driven development: how this method works

1 Answer

+5 votes
by (1.6m points)
edited
 
Best answer

What is test-driven development?
How exactly does test-driven development work?
How is TDD different from other test methods?

image

Test-driven development: how this method works

As you increase the complexity of the software, the agile methodology of the test-driven development (TDD) becomes more and more popular. Not surprising, since TDD allows programmers to ensure that there are no gaps in the software design before writing the source code. This not only significantly increases the quality of the final software , but also reduces maintenance costs..

Test-driven development is used, among other areas, in extreme programming, characterized by reviews , tests, and constant design and redesign. The test-driven development also fits a development cycle , which must be respected to make it effective implementation.

Index
  1. What is test-driven development?
  2. How exactly does test-driven development work?
  3. How is TDD different from other test methods?

What is test-driven development?

Varied methods have long existed for testing the quality of programs. Already in the early days of software development , the functionality of the programs was evaluated in the quality departments by independent testers . Back then, the development of the software itself and the testing procedures were considered separate issues. This perception changed when Kent Beck, the American developer behind extreme programming , launched the concept of test first , which turned the order of tasks around: instead of writing the source code first and then putting it to the test, the development team Write the tests first and then use the test cases to write and implement the code in the best possible way..

If you don't have much knowledge of software development , this process may sound counterintuitive, but it actually makes a lot of sense and works better. While conventional test methods performed subsequently apply a cascade model or V, TDD processes are developed as cycle . More specifically, test cases that often fail are purposely identified first, and then only the code necessary to pass the tests is written. The components are then refactored: keeping the functions, the source code is expanded or restructured, as needed.

How exactly does test-driven development work?

The test-driven development is oriented according to the results of the test cases defined by the developers. Its cyclical structure ensures that code is transmitted to the production system only when all software requirements have been met . In other words, the code elements are refactored and retested as many times as necessary, until the test no longer fails. This strategy allows to enrich the software little by little with new functions, writing new source code after each passed test. For this reason, TDD is considered an incremental model of software development ..

Each test typically takes no more than a few seconds or minutes to cycle through, so results can be seen quickly in production code. In order for repeats to be carried out without added effort, a TDD tool and framework are required . Developers generally use build automation tools , such as CruiseControl or Jenkins, which allow you to integrate components into the source code continuously and without errors. Other popular frameworks and tools for Java development are JUnit, Maven, and Ant. As a general rule, tests are always written in the same programming language as the source code. For PHP there are, among other tools, Ceedling and CMock.

And how exactly are the tests developed? The cycle that programmers follow in test-driven development is also called red-green-refactor and describes each of the phases that must be fulfilled to achieve greater efficiency:

  1. Red phase ( phase network ) : at this stage must step into the shoes of the user who wants to use the code easily. Therefore, a test is written that contains components that have not yet been implemented, to later decide which elements are really necessary for the code to work.
  2. Green phase ( phase green ) : assuming that the test fails and is marked in red, then it adopts the role of programmer and try to find a simple solution. It is very important to write only the amount of code that is necessary. The written code is then integrated into the production code, so that the test is marked in green.
  3. Refactoring : in this step, the productive code is cleaned and its structure is perfected. It is arguably now being completed and restructured in a way that is elegant and understandable to developers. Among other things, duplicates are eliminated in the code, and it becomes more professional.

It should be taken into account that the tasks do not overlap each other, that is, that no tests are written in phases 2 and 3 or productive code is written in phases 1 and 3.

How is TDD different from other test methods?

The test-driven development is a methodology of design software based on test or tests to guide the process. Contrary to methodologies that postpone testing to a later point, TDD test cases are performed early in the design process. The tests used in this methodology differ in terms of purpose and scope: the simplest is the module test or unit test . With it, individual components of a computer program are tested. Other more complex tests are functional and integration tests , which examine the interaction between different parts of the system and the functionality of the software as a whole.

From TDD, a few years ago, behavior-driven development (BDD) or, in Spanish, behavior-guided development was developed . This methodology is not initially guided by the adequacy of the code, but by the behavior that you want to observe in the software . One of the advantages of this strategy is that it does not require specialized coding knowledge to specify the desired test cases, allowing quality managers and other stakeholders to be incorporated into the development process. In general, it can be said that BDD is the best method when designing tests, while TDD results in a cleaner architecture.

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

Advantage Drawbacks
The resulting software is of high quality and has fewer bugs. Requires prior coding knowledge and longer learning time.
The system architecture and production code are easy to understand and well structured. It only checks the adequacy of the code, not the functionality of the software.
Fault identification is fast and maintenance work is reduced. In some cases it must be accompanied by other test methods.
Eliminating redundancies in the code avoids overengineering, that is, unnecessary complexity of the product.  

Although the different test methods can be implemented one by one, applying a combination of test-based and behavior-based methods results in higher quality software , which also provides greater customer satisfaction.


...