What is TDD ❓
To answer this question, we can start talking about its origin...
🚀 Origin
It emerged around 1990, where it was initially introduced by XP (Extreme Programming), by Kent Beck.
The intention was to encourage the development of simple codes, so that they could be checked and validated with an equally simple technique.
As mentioned above, all started thinking about simple codes, and we need keep that thinking.
But, what is the concept behind TDD?
Why is it different from other unit tests?
💡 Concept
Test Driven Development - Methodology which preaches that the tests must be written before the coding itself, different from the traditional model.
Is it!
The main idea is to create small tests based on initial understand and change or write new code only if the test fails, to avoid duplication of tests and code.
But how we can do it?
🔢 Steps
Red: The first step is write the test and at this point the code doesn't exist, so.. the test must fail.
Green: The second step is write the code to pass the created test. The main focus should be on passing the test and not on good practices or the best code.
Refactor: The third and last step is to refactor the code and test. The main point is find the best way to write the both code and tests, avoiding duplicate codes and thinking about DI, SRP, etc.
After understanding the theories about TDD, we need to know which tools can be used to do what we need to do.
Bellow, I have listed some tools for some languages that we can use.
For our examples, I'll using JavaScript for the FrontEnd and .Net for the BackEnd, and the tools choosen for testing were Jest and xUnit respectively.
📚 Libraries
〰️ .Net
- MSTest
- xUnit
- nUnit
📜 JavaScript
- Jest
- Mocha + Chai
♨️ Java
- JUnit
🐘 PHP
- PHPUnit
📘 Examples
✅ Good Practices
To facilitate the creation of tests we can use some patterns, and just below I have listed two of them with my understanding of their steps.
Tests Structures AAA and GWT
Common patterns of writing unit tests, and making code easier to read and understand with 3 steps.
- Given/Arrange - Define a known state;
- When/Act - Run the application;
- Then/Asset - Make sure the new state is the expected state.
📍 Simple Way
Now, we can see the .Net codes using the steps seen earlier.
Step 1 - Red
[Fact]
public void TestsTemperatureConversor()
{
//Given/Arrange
double fahrenheit = 32;
double celsius = 0;
//When/Act
double calculateVal= TemperatureConverter.FahrenheitToCelsius(fahrenheit);
//Then/Assert
Assert.Equal(celsius, calculateVal);
}
//First to fail
public static class TemperatureConverter
{
public static double FahrenheitToCelsius(double temperature)
{
throw new NotImplementedException();
}
}
Step 2 - Green
public static class TemperatureConverter
{
public static double FahrenheitToCelsius(double temperature)
{
return Math.Round((temperature - 32) / 1.8, 2);
}
}
Step 3 - Refactor
/*
It can be refactored or not. If the code is very simple,
there is no need to refactor. In another scenario,
this could be refactored using an external service for example.
*/
public static class TemperatureConverter
{
public static double FahrenheitToCelsius(double temperature)
{
return Math.Round((temperature - 32) / 1.8, 2);
}
}
💪 Practice
The practice code is on Github, take a look. There are the codes of javascript and .net tests.
If you prefer, I can record a video about this article explaining all the steps, including project creation, and post it on YouTube.
Give me feedback on this.
↔️ Pros and Cons
✔️ Pros
- Ability to reduce small bugs
- Cleaner and simpler code, eliminating unnecessary codes
- code reliability
- Favors the documentation process
- Facilitates refactoring
✖️ Cons
- Learning curve
- Testing becomes part of project maintenance
- Makes the project "expensive"
⁉️ Doubts
Comment, bring your doubts and suggestions to improve this and the next contents.
Top comments (0)