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:
- 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.
- 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.
- 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.