Unit testing

In this post, I’ll share stuffs related to Unit testing only. I’ll share examples of Unit testing in later posts.

What is Unit testing?

Unit means a small portion of your code. So Unit testing means test each small-small functionalities of an application. In procedural programming, unit is a whole module but in object oriented programming, it can be a class, an interface or even a method.

Unit test cases

Each unit test cases is different from the other. Each unit test case will handle at least one functionality which needs to be tested. Generally, Unit test cases are written by Software developers / software engineers while software development process. Unit test cases are short code fragment. It is white-box-testing.

Advantages of having unit tests

By having unit test case, An engineer can ensure that piece of functionality is working as expected and as intended.

Each component of application will become independent and testable.  Less coupling and more cohesion.  If components are independent then its easy to replace, modify and test them.

TDD (test-driven development) is ensuring that only necessary production code will be written. First an engineer needs to write test case and then production code. Basically, they need to follow this approach. 1. Red, 2) Green and 3) Refactor.

Unit test cases allows developer to refactor code at later stage in development process and by running test cases, developer can ensure that every functionality is working as expected.

Unit test cases are the living documents of an application. By just looking at unit test case, an individual come to know that which functionalities and what behavior is expected from particular module.

Disadvantages

If developer has not covered all scenarios then it may give wrong indication. E.g. Not wrote unit test cases to cover all scenarios, refactor code, run test cases (not covering refactor code) and boom!

Sometimes, developers gives low priority to unit test cases and not writing clean unit test cases. Result will be to take more time to understand each test case.

If we not follow TDD then first we have to write production code and then unit test case. In this case many times, we need to restructure software components to write unit test cases (E.g. Components dependent on each other) and that involve re-testing of components which may involve significant amount of time.

Challenges in writing unit test cases

  1. Test case name
  2. Finding & injecting dependencies
  3. Wrong test cases

Tools

There are many tools available in market for writing unit test cases. Here I have enlist few tools which are popular in market. There are many other tools also available other than below.

  1. NUnit: .Net platform
  2. Microsoft unit testing framework: .Net platform in visual studio
  3. JUnit: Open-source, java programming language
  4. TestNG: Open-source, Java programming language
  5. Cucumber: End-to-end java unit testing, Javascript, Ruby or Kotlin
  6. PHPUnit: PHP programming language

So whatever tool you used for writing unit test case but the bottom line is to make unit testing automated, fast and accurate! Unit test cases isolate each part of your application and verify whether all parts are working properly or not.

Unit testing
Credit: https://www.guru99.com/

Soon, I will share examples on Unit testing with mock, stub and without mock using Microsoft unit testing framework. Happy coding!

2 thoughts to “Unit testing”

Leave a Reply

Your email address will not be published. Required fields are marked *