Whether you develop a small project or a large project, testing the code is a must these days. If you publish your code without testing, you might not get a problem straight away, but the worst problems are the ones you overlooked. With TDD (Test-driven Development) the probability of a potential/future problem is prevented.
What is TDD, Test-driven Development?
In a nutshell: With Test-driven development the tests are created before codes. The code is developed step-by-step based on these tests. This avoids unnecessary codes/functions/classes.
TDD by Kent Beck
The Unit Tests and units (codes) are always developed in parallel. The actual programming takes place in small, repeated micro-iterations. Such an iteration, which should only last a few minutes, has three main parts, which are called the Red, Green, Refactoring.
Red : Write a test that should check a new behavior to be programmed (functionality). You start with the simplest example. This test is initially not fulfilled by the existing program code, so it must fail.
Green : Change the program code with as little effort as possible and add to it until it passes all tests.
Then clean up the code (Refactoring): Remove repetitions (code duplication), abstract where necessary, align it according to the binding code conventions, etc. After each change, the tests are carried out and failing is prohibited to adopt the apparently incorrect change in the code used. The aim of the cleanup is to make the code simple and understandable.
Stub objects
The stub objects are the test dummies (external files, Json, XML etc.) For example, if a class needs the customer data from an external interface (other class, database, external app), you can use stub objects to simulate these data.
Mock objects
The mock objects are the internal codes. So that the project works independently of external interfaces in the local environment, the existing codes that communicate with external interfaces are simulated with their own test codes.
Advantages of test-driven development
- no untested code, so you can sleep peacefully 🙂
- clean code architecture by TDD
- no redundancies due to refactoring
Test-driven development mostly focuses on unit testing. There are other types of testing as well as Behavior Driven Development . But the basic idea of all testing concepts is testing 🙂
In the next article I will create a unit test and an acceptance test with Codeception . Until then…