How To Create xUnit Test In Visual Studio 2022

Introduction

Unit Testing is a software testing approach or technique that is performed at development time at the developer side to test the smallest component of any application software. It means rather than testing the big application software module in one go, a developer can test the small part of that application software module. A small application software component in the sense that it could be any property, function, or any class which is handling the specific functionality.

Unit test cases help us to test application software and find out whether the individual unit is performing the task in a good manner or not. It basically ensures us that the application is designed as per the business requirements and helps us to figure out the bugs before deploying the actual code to the QA environment.

Scenario

In this example, we will create MyCalculation.cs class with the add method. This add method will take two integer parameters and return the addition of both the parameter as an integer. Now call this add method in the main class by creating the object of the MyCalculation class and passing two integer parameters. 

Create a new project XUnitTestProject, by default UnitTest1.cs class, will create. Now add the project reference in XUnitTestProject create the object of class MyCalculation and call the add method. Now create the PassingTest() and FailingTest() two methods and pass parameters as examples. With the help of Assert.Equal() method check the test case result. Assert.Equal() will take two-parameter, the first parameter compares with the second parameter, and give the appropriate result. If both the parameter is the same it will return true else return false.

Implementation 

Step 1

Selecting Solution > Add > New Project > Visual C# > .NET Core, then select either the xUnit Test Project (.NET Core) project template.

Visual Studio - 2017

How to create xUnit test in visual studio

Visual Studio - 2022

How to create xUnit test in visual studio

Step 2

New project added as xUnit project( XUnitTestProject) in solution

How to create xUnit test in visual studio

Step 3

Need to add the following xUnit Package 

Install-Package xunit
Install-Package more.xunit.runner.visualstudio

Go to Tools =>NuGet Package Manager=>Manage NuGet Package for Solution.

How to create xUnit test in visual studio

How to create xUnit test in visual studio

How to create xUnit test in visual studio

How to create xUnit test in visual studio

Step 4

Write the test case 

How to create xUnit test in visual studio

MyCalculation.cs 

using System;
namespace Calculater
{
    public class MyCalculation
    {
        public int Add(int x, int y)
        {
            return x + y;
        }
    }
}

Program.cs

using System;
namespace Calculater
{
    internal class Program
    {
        static void Main(string[] args)
        {
            MyCalculation objMyCal = new MyCalculation();
            int answer = objMyCal.Add(3,5);  //Add(3,5);
            Console.WriteLine("Addition="+ answer);
        }
    }
}

UnitTest1.cs

using System;
using System.Collections.Generic;
using Xunit;
using Calculater;
namespace XUnitTestProject
{
    public class UnitTest1
    {

        MyCalculation objMyCal = new MyCalculation();
        [Fact]
        public void PassingTest()
        {
            Assert.Equal(4, objMyCal.Add(2, 2));
        }

        [Fact]
        public void FailingTest()
        {
            Assert.Equal(5, objMyCal.Add(2, 2));
        }
    }
}

Step 5

Now let's run all the xUnit unit test cases as in the screen

How to create xUnit test in visual studio

How to create xUnit test in visual studio

 

Note
The attached zip code you can download for your reference, attached code support only in visual studio 2022.

Summary

In this article, we have seen the step-by-step implementation of the xUnit.

I hope this helps. If this helps you then share it with others.

Sharing is caring! :)


Similar Articles