DEV Community

Cover image for Integration of unit testing with ASP.NET Core - iFour
Harshal Suthar
Harshal Suthar

Posted on • Originally published at ifourtechnolab.com

Integration of unit testing with ASP.NET Core - iFour

Unit testing is used to test individual components or units to determine if there are any issues by the developer himself. The unit testing is used to validate that each component of the software code performs as expected. The unit testing is concerned with the functional correctness of standalone modules and it is done by the developer during the development phase of an application.

Why do we need unit tests?

Unit testing is important because the software can fail in unexpected ways. Manual testing is slow, least reliable, and expensive to test an application. Whereas, unit testing saves time and money if it is implemented properly.

Here, are a few key reasons to perform unit testing:

  • Unit tes

ts are used to fix bugs early in the development phase and save costs and time.

  • It is used by the developers to understand the code base and then it enables them to make changes quickly.
  • Good unit testing serves as project documentation of software.
  • Unit tests are used to re-use code and they are easy as well to identify defects in any application.

Unit Testing Tools

There are many automated tools available to test the application.

Few examples as below:

1) Junit

Junit is a free testing tool used by the developer for Java programming language. It implements assertions to identify the test method. Junit is used to test data first and then inserted in the piece of code.

2) NUnit

NUnit is a framework widely used by the developer to unit-test an application that is developed in all .NET languages. Nunit is open source and supports data-driven tests that run parallelly. Nunit allows the developer to write a script manually.

3) PHPUnit

PHPUnit is a unit testing tool used by the developer to test an application. PHPUnit tests a small portion of code known as units. PHPUnit provides a pre-define assertion to assert that the system behaves in a proper manner.

4) JMockit

JMockit is open-source Unit testing with line and path metrics. JMockit allows the developer to record and verifying syntax in API and offers Line coverage, Path Coverage, and Data Coverage.

5) EMMA

EMMA is an open-source toolkit used by the developer for analyzing and reporting code written in Java language. Emma is a java-based external library and support method, line, basic block coverage type.

Setting up Unit testing in ASP.NET Core Application

There are many test frameworks available in the market but we will be going to use the xUnit, which is a very popular testing framework. The simplest way to create unit test case for an ASP.NET Core web app project is to create a new test project using a template.

We can create a test class for each application class. The simple unit test includes three-step:

  • Arrange: In this step setup the necessary variable and object.
  • Act: In this step, call the method with a required parameter for testing
  • Assert: In this step, the expected result is verified

Read More: How To Secure Asp.net Core Web App?

Create a sample ASP.NET Core application

To create ASP.NET Core Console Application follow the below steps:

Step 1: Open Visual Studio, select File option -> New option -> Project

Step 2: elect the ASP.NET Core Console Application template and click Next.

Step 3: Enter the project name, and then click Create.

Image description

Step 4: Create a new class

Add new class Calculator.cs


namespace UniTestDemo
{
    public static class Calculator
    {
        public static double Addition(double num1, double num2)
        {
            return (num1 + num2);
        }

        public static double Subtraction(double num1, double num2)
        {
            return (num1 - num2);
        }

        public static double Multiplication(double num1, double num2)
        {
            return (num1 * num2);
        }

        public static double Division(double num1, double num2)
        {
            return (num1 / num2);
        }
    }
}

Enter fullscreen mode Exit fullscreen mode

Now, Add Testing Project

1: In Visual Studio 2019, Right-click on solution-> Add -> Project.

2: In the Create New Project dialog, select the Test ->xUnit Test Project (.Net Core) template, and click Next.

3: Enter the project name, and then click Create.

Image description

Step 5: Add Reference to your project

Right-click on your Testing project (XTestDEmo)->Add->reference

Image description

Searching for Dedicated ASP.Net Core Web Developer? Your Search ends here.

Step 6: Now, Add one class in 'XTestDEmo' project named 'TestCalculator.cs'

In 'TestCalculator.cs' class we will write test case for method define in 'Calculator.cs'. These methods follow three-step: Arrange, Act, Assert.

The following class contains test cases for Calculator:


using System;
using UniTestDemo;
using Xunit;

namespace XTestDEmo
{
    public class TestCalculator
    {
        [Fact]
        public void Test_Addition()
        {
            // Arrange  
            var number1 = 4.9;
            var number2 = 3.1;
            var expectedValue = 8;

            // Act  
            var sum = Calculator.Addition(number1, number2);

            //Assert  
            Assert.Equal(expectedValue, sum, 1);
        }

        [Fact]
        public void Task_Subtract_TwoNumber()
        {
            // Arrange  
            var number1 = 3.2;
            var number2 = 1.5;
            var expectedValue = 1.7;

            // Act  
            var sub = Calculator.Subtraction(number1, number2);

            //Assert  
            Assert.Equal(expectedValue, sub, 1);
        }

        [Fact]
        public void Test_Multiplication()
        {
            // Arrange  
            var number1 = 2.5;
            var number2 = 3.5;
            var expectedValue = 8.75;

            // Act  
            var mult = Calculator.Multiplication(number1, number2);

            //Assert  
            Assert.Equal(expectedValue, mult, 2);
        }

        [Fact]
        public void Test_division()
        {
            // Arrange  
            var number1 = 2.9;
            var number2 = 3.1;
            var expectedValue = 0.94; //Rounded value  

            // Act  
            var div = Calculator.Division(number1, number2);

            //Assert  
            Assert.Equal(expectedValue, div, 2);
        }
    }
}


Enter fullscreen mode Exit fullscreen mode

Step 7: Now, Run your test cases

For running all test cases open Test Explorer where you will find you're all test case. To run click on RunAll this will start executing test cases.

To open Test Explorer in visual studio Click on Test option->Window->Test Explorer.

Image description

Once your test case runs successfully, the output screen will be shown as below.

Image description

Conclusion

In this blog, we have discussed the unit testing with ASP.NET which is used by the developer for issuing defects and we have also portrayed the purpose of unit testing. We illustrated a simple example of a unit test with Xunit using the ASP.NET Console application. We hope you will acquire a clear understanding on how to integrate unit testing.

Top comments (0)