Last Updated: September 20, 2020

Nesting test cases with kotlin

Description: As features and requirements grows in our application, code and test cases grows hand in hand. It becomes very difficult to track the test cases with lots of them in single class. 

Consider a sample unit test class in Kotlin with bunch of test cases in single class

class ClientTest {

@Test
fun create(){
assert(true)
}
@Test
fun delete(){
assert(true)
}

@Test
fun connected(){
assert(true)
}

@Test
fun disconnected() {
assert(true)
}
}
fig.1

                                                        fig. 1

Well nothing wrong with test cases. 

Just imagine if you have 100+ tests within fig.1

By looking at fig.2 results it becomes difficult to figure out,

1. What exactly does these tests do ? or 

2. What's the logical connection within unit tests? etc 

That's where nesting comes useful for developers.

Consider a nesting sample with Kotlin

class ClientTest {

@Nested
inner class Operations{
@Test
fun create(){
assert(true)
}
@Test
fun delete(){
assert(true)
}
}

@Nested
inner class Client {
@Test
fun connected(){
assert(true)
}

@Test
fun disconnected(){
assert(true)
}
}
}

                                                   fig. 3

                                                     
                                                    fig. 4

We can now clearly make some distinction that, class in connecting to a client and doing some operations. (fig. 3 and fig. 4)

Although nesting is just a logical separation of test cases, it's very handy for developers in large scale projects.

For more updates follow us on -  Twitter

#codingIsAnArt #coderconsole


9 comments :

  1. Thanks for sharing this code. It will be more important and informative.

    Ac Repair Dubai

    ReplyDelete
  2. This is such a great resource that you are providing and you give it away for free. I love seeing blog that understand the value of providing a quality resource for free.

    Jecob

    ReplyDelete
  3. There is no way to get this useful info . Its very fabulous and easy for me to get it from WhatsApp Aero. I am very thankful and I appreciate your intelligent work. Awesome!!

    ReplyDelete
  4. Thanks for the detailed article on this topic. I would like to see more such awesome articles from you. Download GBWhatsApp APK

    ReplyDelete
  5. Thank you for sharing this. It was helpful. Keep sharing such things.
    Also I like to play COC MOD Apk which is more fun than any other game.

    ReplyDelete
  6. i never know the use of adobe shadow until i saw this post. thank you for this! this is very helpful. Mason Jacob

    ReplyDelete
  7. A very awesome blog post. We are really grateful for your blog post. You will find a lot of approaches after visiting your post. trello
    wattpad
    drupal
    slashdot
    seekingalpha

    ReplyDelete
  8. Hey, you can download the other version of this system, it is equipped with unlimited features. Also check out minecraft hack download it will provide you apk file with lot's of premium features.

    ReplyDelete
  9. Infycle Technologies offers couples for care and technology in addition to Python Training in Chennai, 100% of the internship class will be prepared. After completing the training, the participants will be sent to the upper MNCs interviews. Call 750633333 to get more information and get a free display.

    ReplyDelete

Your comments are valuable for us !!!