What Exactly is Unit Testing?

ag2byte

Abhigyan Gautam

Posted on June 19, 2023

What Exactly is Unit Testing?

One of the things that developers don't like doing is writing test cases for their code, yet it is one of the most important steps that ensures code quality of small units of the project. In this series we will look into what Unit Tests are and how can we setup a simple Unit Testing module in C#. Please note that there are several ways to do it, I am just trying to share what I've learnt. Feel free to add to it!

The main objective of unit testing is to isolate and validate each unit of code independently, verifying that it behaves as expected and produces the desired output given specific inputs. Since the units are tested independently, developers can detect the bugs early in the development process making it easier to identify and fix before they have a huge impact on the project.

For example, let's consider a simple function as follows:

...
...
public static int Factorial(int n)
    {
        if (n < 0)
            throw new ArgumentException("Factorial is not defined for negative numbers.");

        int result = 1;
        for (int i = 1; i <= n; i++)
        {
            result *= i;
        }

        return result;
    }
...
...
Enter fullscreen mode Exit fullscreen mode

The above function Factorial() is a unit in this case. For the function to return correct response, the following test cases can be thought of:

  • Factorial of 0 is 1
  • Factorial of positive number is correct.Example Factorial(5) should return 120
  • Factorial of Negative Number should throw Exception, in this case Argument Exception.

The general idea is that test cases like this should return the respective responses. This is called Assertion.

In fact, the three main steps of Unit Test are :

  • Arrange - The necessary preconditions and inputs for the Unit to be tested are prepared. In the above case the numbers to be tested are a part of this step
  • Act - The test is called using the above arranged items. In the above case calling the function Factorial() with the numbers represents this step.
  • Assert - The observed output is compared with the expected output and the test is marked as correct or failed based on the output. In the above case Factorial(5) gives 120 represents this step.

Unit tests should cover various scenarios, including both expected and edge cases, to provide comprehensive coverage of the unit's behaviour. By having a suite of well-designed and thorough unit tests, developers gain confidence in their code and can easily detect regressions or unintended side effects when making changes or refactoring code.

How is this different from Integration Testing?

Unit Test works on small parts of code at one time. Integration Testing is the end to end testing of these units all at once. Integration Testing gives the report of how where these so called units can work together while Unit Test give the accuracy of these individual units.

unit-integrationtest

💖 💪 🙅 🚩
ag2byte
Abhigyan Gautam

Posted on June 19, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Setting up the Project for Testing
unittest Setting up the Project for Testing

June 19, 2023

What Exactly is Unit Testing?
unittest What Exactly is Unit Testing?

June 19, 2023