Unit testing in ASP.NET MVC 2023:-

What is Unit Testing?

Unit testing is testing of each and every individual parts of the program.For eg:-The individual part is a method inside the class. Unit tests are generally done by developers.After developer finishes his unit testing , it goes to testing team for full round of testing of the application.

why unit tests?


In order to minimize the bugs,before it gets tested by the testing team.When change request received for the existing applications. The testers will do regression testing again and again, if any changes introduced in production environment.

The framework used for unit testing in C#:

MsTest
NUNit

The unit test cases has 3 statements:-

1.Arrange
2.Act
3.Assert

In Nunit, it shows more informations when the test got failed.we can define multiple tests as decorations like this,
[Test,TestCase(1,2,3)]
[TestCase(3, 8, 11)]
[TestCase(2, 3, 11)]
[TestCase(4, 2, 6)]
public void TestMethod1(int a,int b,int res)
{
int actual;
int expected = res;
Example1NUnit objtestunit = new Example1NUnit();
actual=objtestunit.Add(a, b);
NUnit.Framework.Assert.AreEqual(expected, actual);
}

NUnit:-

It is an open source unit testing framework which can be used for testing .Net languages. Nunit can be installed in Visual studio by using Nuget packages. Let us create a sample unit testing application using NUNIT:

Step 1: Create a blank solution

Step 2: Now add class library for mathematical calculations called Example1NUnitLib.

Step 3: Now add Nunitexample project in the solution.

Step 4: Install Nunit and NUnit3TestAdapter in your unit testing project.

If you don’t install Nunit3TestAdapter then the results will not be shown in TestExplorer.

Step 5: Both Nunit and NUnit3TestAdapter should be installed from manage nuget packages.

After adding in unittesting project, you will see Nunit.framework in references of the project.but you will never seen Nunit3TestAdapter, But internally it does the work for showing the TESTEXPLORER WINDOW.

Step 5: Add Nunittest class inside that project.

The class library for Mathematical calculations is Example1NUnit class. (i.e Example1NUnitLib.dll)

Public class Example1NUnit
{
public int Add(int I,int j)
{
return i+j;
}
public int Sub(int I,int j)
{
return i-j;
}
}

Now call this Example1NUnit.dll in NUnitExampleProject.
Decorate the Nunittest class with [TestFixture] and decorate method with [Test] like this
Using example1NUnitLib;
Using Nunit.Framework;
namespace NUNITTestingEXAMPLE
[TestFixture]
public class Nunittest
{
[Test,TestCase(1,2,3)]
public void TestMethod1(int a,int b,int res)
{
int actual;
int expected = res;
Example1NUnit objtestexample= new Example1NUnit();
actual= objtestexample.Add(a,b);
Nunit.Framework.Assert.AreEqual(expected,actual);
}
}

NUNITTestingEXAMPLE project should be class library. Don’t execute the application it is not windows application or console application.

Step 6: Now Run the test by selecting Test menu-> Run-> AllTests

Now the test results are opened in Test Explorer.You can see Test Explorer in left side of this picture.
Open Test Explorer : Test->Windows->TestExplorer