Asp.Net Core – First xUnit Test Case

Unit Testing is universally accepted practise to test the application automatically instead of running manually and test each scenario. There are different testing frameworks like xUnt, NUnit, and MSTest available for C# and may require to install plugins in Visual Studio to run the tests.

Test projects are console apps, but there is no Program class and static void main method, it looks like class library. When you run test cases, .Net SDK automatically injects Program class and all test methods will get called in static main method and execute.

Today, we will create simple xUnit test project to test one of our class Currency converter. We have simple class Currency as shown below.

namespace Asp.NetCoreWebApp.Models

{

    publicclassCurrency

    {

        publicdecimal ConvertToUSD(decimal value, decimal rate, int decimalPlaces)

        {

            var valueInUSD = value / rate;

            returndecimal.Round(valueInUSD, decimalPlaces);

        }

    }

}

To add xUnit test project, right-click on solution => Add => New Project and xUnit Test Project as shown below.

Graphical user interface, text, application, email

Description automatically generated

Rename the default unit test class to CurrencyTest. Right-click on the Dependencies folder under test project and Add Project Reference to your application project for which classes you want to write unit tests. Below is the Unit Test class to test Currency class method ConvertToUSD().

using Asp.NetCoreWebApp.Models;

using Xunit;

 

namespace Asp.NetCoreWebApp.Tests

{

    publicclassCurrencyTest

    {

 

        [Fact]

        publicvoid ConvertToUSDTest()

        {

            //Arrange

            var currency = new Currency();

            int decimalPlaces = 2;

            decimal value = 100, rate = 1.3m;

            decimal expected = 76.92m;

 

            //Act

            var actual = currency.ConvertToUSD(value, rate, decimalPlaces);

 

            //Assert

           Assert.Equal(expected, actual);

        }     

    }

}

 

Test class should be public and non-static. Each test method should be public, void, and denoted by [Fact] attribute as shown above. Here we written the code to test positive scenario. Let’s run the all test cases by going to Test menu and select Run All Tests or simply by pressing Ctrl+R, A from keyboard.

Each test method configured using AAA (Arrange, Act, Assert) style. In Arrange, we define all parameters with values and defines instance for testing class. In Act stage, we call the method and captures the result. In Assert, we verifies the result which we captured in Act stage.

By selecting Run All Tests option, all our projects will get build and executes all test cases. It displays result for all our test cases as shown below (here we have only one test case to execute).

Graphical user interface, text, application

Description automatically generated

If the test runs successfully, it displays in green and in red if it fails. Here our test case produces the expected result and is displayed in green.