Unit testing in C# .Net Core using NUnit

What is Unit Test?

  • It is a software development process in which we can test the smallest code of the application.
  • This is an important step in software development. It detects early flaws in code.
  • It is a component of TDD(Test Driven Development).

Advantages of unit testing

  • Easy Debugging.
  • Easy code migration or reuse.
  • Identify a problem or bug in the early stage.
  • Fix problems or bugs earlier.
  • The developer can easily change the code.

When we write test cases, we need some sort of pattern. We can use the AAA pattern, which is famous for testing. It stands for Arrange, Act, Assert.

Arrange: It is a setup phase. we can set up basic items for a test

  • e.g. Create object instances, Test data.

Act: Execute main application code, calling methods, or properties.

Assert: It checks test results. Pass/Fail,

Now will see an example.

1. Create a .NET Core Empty Solution

First, create a new .NET Core empty solution using Visual Studio.

createNew

After selecting an empty solution, provide the solution name and click on Create button.

configure

github

2. Now, add a new project as a Class Library

a) right click on the project name to Add, click on New Project.

classLibrary

b) search the class library and select the class library project and click on the Next Button.

nuLibfrary

c) after clicking on the Next button, Add the Project Name and click on the Next button.

addProject

d) Select the target framework we are selecting.Net5.0.

selectFramework

3. Now again, we are adding Class Library Project as Unit Test Project

a) right click on solutionselectAdd, click on New Project.

newProjwect

b) search the class library and select the class library project and click on the Next Button.

nuLibfrary

c) after clicking on the Next button, Add the Project Name and click on the Next button.

projectName

d) Select the target framework we are selecting.Net5.0.

verifyFramework

4. Need to Add below NuGet Packages

dotnet add package NUnit
dotnet add package NUnit3TestAdapter
dotnet add package Microsoft.NET.Test.Sdk

Nuget netsdk

5. Add a new ClassArithmeticOperationTest.cs

 [TestFixture]
    public class ArithmeticOperationTest
    {
        [Test]
        public void AdditionOfTwoNumber_InputTwoInt_GetCorrectAddition()
        {
            //Arrange
            ArithmeticOperation calc = new();

            //Act
            int result = calc.AdditionOfTwoNumber(10, 20);

            //Assert
            Assert.AreEqual(30, result);
        }

        [Test]
        public void AdditionOfTwoNumber_InputTwoInt_GetWrongAddition()
        {
            //Arrange
            ArithmeticOperation calc = new();

            //Act
            int result = calc.AdditionOfTwoNumber(10, 20);

            //Assert
            Assert.AreEqual(40, result);
        }



    }

In the above code snippet, we created 2 methods.

1. AdditionOfTwoNumber_InputTwoInt_GetCorrectAddition

In this method, we arranged data 10,20 and expected output 30, so this test case is Pass.

2. AdditionOfTwoNumber_InputTwoInt_GetWrongAddition

In this method, we arranged data 10,20 and expected output 40, so this test case is Fail.

summary

Please do leave a comment if you find it useful. You can find the repository here.


Similar Articles