Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Unit Tests : Writing Readable Tests
DevByJESUS
DevByJESUS

Posted on

     

Unit Tests : Writing Readable Tests

Hello 😄
It's me Again
Today we will finish our series about advices for great unit tests with a long post (not so long) about writingREADABLE 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 thefuture readers, and here we are coming in the universe of CLEAN CODE. 😗

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

let's start

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 thisWhen I call method X with a null value , you notice thewith 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 thisWhen I call method X with a null value, then it should do Y. , when we see thethen 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 TheClean Code Book
Two differents block of code , and the readability is not the same

  1. Bad 💔
[Test]public void BadlyNamedTest(){   LogAnalyzer log = new LogAnalyzer();   int result= log.GetLineCount("abc.txt");   Assert.AreEqual(-100,result);}
Enter fullscreen modeExit fullscreen mode
  1. Good 💚
[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);}
Enter fullscreen modeExit fullscreen mode

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

//arrangevar repository = Substitute.For<IClientRepository>();var client = new Client(repository);// actclient.Save();// assertmock.Received.SomeMethod();
Enter fullscreen modeExit fullscreen mode

1. Bad 💔

[Test]public void BadAssertMessage(){   //some code here    Assert.AreEqual(COULD_NOT_READ_FILE,       log.GetLineCount("abc.txt"));}
Enter fullscreen modeExit fullscreen mode

This is bad because there is no separation between theAct and theAssert

2. Good 💚

[Test]public void BadAssertMessage(){   //some code here   int result= log.GetLineCount("abc.txt");   Assert.AreEqual(COULD_NOT_READ_FILE,result);}
Enter fullscreen modeExit fullscreen mode

Better isn't it ? 😆

You can go further withthis great article

That's all ? Yes we are talking about unit tests not your production code , for production code and daily basis I recommend you againThis Book

Thanks for reading 😊
By The Grace of JESUS❤️ i will write some posts for the part 4 of the BookThe Art Of Unit Testing

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

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

I want to change the world with code by having fun with it . #keeplearning #cleancode #tdd #atdd #bdd #ddd
  • Location
    Dakar , Senegal , Africa
  • Joined

More fromDevByJESUS

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp