Do You Know What is TDD?
Tags: tdd, testing, backend, frontend
Published: 2023-06-07 14:47:52

## What is TDD :question:

To answer this question, we can start talking about its origin...

### :rocket: 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?

### :bulb: Concept

> _**Test Driven Development** - Methodology which preaches that the tests must be written before the coding itself, different from the traditional model._

![Write the test before the code?](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iieuvwcl2w5dis2vtwr3.png)

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?

### :1234: Steps

![TDD Steps](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xmpaw63odevw88dfbrgx.png)

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

## :books: Libraries

#### :wavy_dash: .Net
 - MSTest
 - xUnit
 - nUnit

#### :scroll: JavaScript
 - Jest
 - Mocha + Chai

#### :hotsprings: Java
 - JUnit

#### :elephant: PHP
 - PHPUnit

---

## :blue_book: Examples

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

### :round_pushpin: Simple Way

Now, we can see the .Net codes using the steps seen earlier.

##### Step 1 - Red

```c#
[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

```c#
public static class TemperatureConverter
{
    public static double FahrenheitToCelsius(double temperature)
    {
        return Math.Round((temperature - 32) / 1.8, 2);
    }
}
```

##### Step 3 - Refactor

```c#
/*
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);
    }
}
```

---

## :muscle: Practice

The practice code is on [Github](https://github.com/JVidalN/tdd-presentation), 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.

## :left_right_arrow: Pros and Cons

### :heavy_check_mark: Pros
- Ability to reduce small bugs
- Cleaner and simpler code, eliminating unnecessary codes
- code reliability
- Favors the documentation process
- Facilitates refactoring

### :heavy_multiplication_x: Cons
- Learning curve
- Testing becomes part of project maintenance
- Makes the project "expensive"

---

## :interrobang: Doubts

Comment, bring your doubts and suggestions to improve this and the next contents.

---

## :link: Links
1. [GitHub Repository](https://github.com/JVidalN/tdd-presentation)
2. [YouTube Presentation (Portuguese)](https://www.youtube.com/watch?v=g0rL1Vpd8fU)