What is TDD (Test Driven Development)? How was it?

ibrahimcanerdogan
5 min readNov 16, 2022

--

Photo by Jason Strull on Unsplash

TDD is a programming technique invented by Kent Beck as part of Extreme Programming.
The TDD nomenclature is derived from the abbreviation of the word “Test Driven Development”. As it can be understood, it means that the developed software is developed together with testing. It is a software development approach in which test cases are developed to determine and validate what the code will do.
TDD structure, which is used in Agile software development processes, aims to write more secure tests in software development processes.

TDD Steps

The steps given below are the operations of the TDD loop.
1. A test is written.
2. The test fails.
3. The test is made successful.
4. It is ensured that all existing tests are successful.
5. The code is refactored. In other words, improvement and (or) cleaning is done in the code.

When we apply all the steps in order, before writing the desired development function code, it is necessary to write test codes that allow it to give outputs in accordance with the desired inputs.

TDD Steps within the Project

The TDD steps given above within a software project span the development process of the project. In this process, the writing of the first tests is developed before the coding phase and the other steps are developed after.
1. Design
The design phase is the phase where the development to be made in the project is considered and all the scenarios that will be affected by the development are processed.
2. Test
It is the phase in which the required outputs are compared by giving the necessary inputs to a function that we do not have over the designed scenario, and it is checked whether the functions can be called or not. Since there is no function to be tested at this stage, the tests actually come out wrong. The important thing is to write the tests that we get the necessary outputs from the function we will write.
3. Development
In the development phase, as always, it is the phase of adding a new feature and/or correcting the existing operation.
4. Test
At this stage, the changes made during the development phase are now added to the written tests. Failed tests should be converted to successful ones. If there are redundant or incorrect code additions, they should be deleted from the changes, they should be corrected (refactor).
In simple terms, test cases are created and tested for each function first. At this stage, the code is rewritten to make the failed tests pass and make the code simple and error-free.

TDD Lifecycle

TDD lifecycle basically consists of 3 stages.

  1. Before development, it is necessary to write tests that will result in errors. These tests should not contain any concrete function, but should include tests over the scenario scenario in the design. Thanks to the concrete developments to be made afterwards, the tests are successfully completed by getting rid of their mistakes. As a result of the changes made, the desired values ​​should be obtained under the expected conditions. After it is certain that the desired values ​​have been obtained, the “Refactor” phase is passed. TDD life cycle ends when the code we write is kept simpler to organize, better naming or using a more suitable feature.
  2. Writing erroneous tests before development and succeeding after these tests are mandatory phases in the TDD lifecycle. However, it is not obligatory to have areas for improvement in the written codes, they must be checked. Refactor does not mean “any code change”. Instead, refactoring means that we change the internal structure of the unit under test without changing its external behavior.
  3. This cycle allows writing safe and more efficient code in development. However, there is a certain time and labor cost. Its implementation may not always make a significant contribution.

Benefits

  • It requires a new operation to be planned in the software before the development codes are written. Such a planning ensures that only the necessary code structures (class, function, etc.) are created. Thanks to TDD, these required code structures are tested and appropriate improvements are made. Other enhancements are avoided.
  • A new development and/or correction of a functioning (bug-fix) must act in conjunction with the intervened operation. Writing tests that follow this process will not only provide a more holistic test process by writing less test code, but also test covarge will be increased faster.

Since TDD sets the limits of the code with tests, errors are limited.

  • Tests can serve as documentation because they give many technical information such as which inputs and outputs the codes will give, which inputs, which type they take, which outputs.
  • TDD helps to make the most suitable development for the structure of the project, as it enables the pre-planning of the operation.

Alternatives

As an alternative to the TDD approach, there are different test development approaches such as BDD and ATDD. While BDD is a behavior-oriented test development, ATDD is a development based on acceptance testing.

Conclusion

As a result, TDD; We can summarize it as a practice that allows us to write simple code, design simple, and refactor regularly. With this approach, the limits of developments in a software are more clear and it allows programming in accordance with the “Keep It Simple” approach. In this way, in the project where there are more reliable code structures, both the intelligibility will increase and the project structure will be documented with codes.

IBRAHIM CAN ERDOGAN

Linkedin: https://www.linkedin.com/in/ibrahimcanerdogan/

--

--

ibrahimcanerdogan
ibrahimcanerdogan

Written by ibrahimcanerdogan

Hi, My name is Ibrahim, I am developing ebebek android app within Ebebek. I publish various articles in the field of programming and self-improvement.

No responses yet