Unit Tests : Writing Readable Tests
Tags: testing, unittest, tdd
Published: 2021-12-05 19:31:52

Hello :smile: 
It's me Again 
Today we will finish our series about advices for great unit tests with a long post (not so long) about writing **READABLE UNIT TESTS**.
So Let's go 


## Why Readable Unit tests ?
This question is tricky in our community some people thinks that we can go without it and others think that we should go with it . But the fact is every newspaper can transmit informations where is the difference ( apart from the price ) a great newspaper take care of the design and the others just put the informations there . In development world it is some kind of same some just put their code as long as it runs it is fine , but another group thinks that make the code runs is not enough we should represent it well for the __future readers__, and here we are coming in the universe of CLEAN CODE.  :kissing:

You've guess it , I am with the group about CLEAN CODE.
Ready ? So how my unit tests can be clan ?

![let's start](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iurnup0z78z0vo1pkugd.gif)



## 1. Naming Unit tests
Here the pattern is quite simple , in the name of our unit tests we should be able to respond to :

1. __The name of the method being tested__
This is what Roy Osherove say : 
>This is essential, so that you can easily see
where the tested logic is

2. __The scenario under which it’s being tested__
with a sentence like this **When I call method X with a null value** , you notice the __with a null value__ , we can know the scenario of our unit test.

3. __The expected behavior when the scenario is invoked__ 
With a sentence like this **When I call method X with a null value, then it should do Y.** , when we see the __then it should do Y__ without reading the body of the unit test we can know its expected final behaviour.


## 2. Naming Variables
I can not go deeper in this part , i have read a great book about it The [Clean Code Book](https://www.amazon.fr/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882)
Two differents block of code , and the readability is not the same 

1. Bad :broken_heart:
```
[Test]
public void BadlyNamedTest()
{
   LogAnalyzer log = new LogAnalyzer();
   int result= log.GetLineCount("abc.txt");
   Assert.AreEqual(-100,result);
}

```

2. Good :green_heart:
```
[Test]
public void BadlyNamedTest()
{
   LogAnalyzer log = new LogAnalyzer();
   int result= log.GetLineCount("abc.txt");
   const int COULD_NOT_READ_FILE = -100;
   Assert.AreEqual(COULD_NOT_READ_FILE,result);
}

```


## 3. Separating Asserts from Actions
Again Here to be near this advice we can write our unit test with the AAA ( Arrange - Act - Assert ) Pattern , following this we can be sure that we are going to separate our actions from our asserts. Look at the code below 

```
//arrange
var repository = Substitute.For<IClientRepository>();
var client = new Client(repository);

// act
client.Save();

// assert
mock.Received.SomeMethod();
```
### 1. Bad :broken_heart:
```
[Test]
public void BadAssertMessage()
{
   //some code here
    Assert.AreEqual(COULD_NOT_READ_FILE,
       log.GetLineCount("abc.txt"));
}
```
This is bad because there is no separation between the __Act__ and the __Assert__

### 2. Good :green_heart:

```
[Test]
public void BadAssertMessage()
{
   //some code here
   int result= log.GetLineCount("abc.txt");
   Assert.AreEqual(COULD_NOT_READ_FILE,result);
}
```
Better isn't it ? :satisfied:

You can go further with [this great article](https://medium.com/@pjbgf/title-testing-code-ocd-and-the-aaa-pattern-df453975ab80)


That's all ? Yes we are talking about unit tests not your production code , for production code and daily basis I recommend you again [This Book](https://www.amazon.fr/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882)


Thanks for reading :blush:
By The Grace of JESUS:heart: i will write some posts for the part 4 of the Book [The Art Of Unit Testing](https://www.amazon.fr/Art-Unit-Testing-examples-English-ebook/dp/B097826FLN/ref=sr_1_1?__mk_fr_FR=%C3%85M%C3%85%C5%BD%C3%95%C3%91&keywords=the+art+of+unit+testing&qid=1638731746&sr=8-1)

If you want to contact me , my email : devbyjesus@gmail.com