C# Unit Test Project - Simple Example

Unit test is the code block you write to test the code you have developed. Unit testing is the process of testing to ensure that the code is working properly.


This C# unit testing article will teach you how to create unit tests by using Visual Studio.


Also you will get a basic introduction to Unit Test.


The goal of unit testing is to seperate each part of the application and show that parts are correct.
(A unit is the smallest testable part of a software.)


Unit Testing finds problems early in the development cycle.


Through Unit Testing, you can understand how to work the console application.


Three of the top frameworks for unit testing in C# are:

- MSTest/Visual Studio
- NUnit
- xUnit.NET


Create Console Application


- Open Visual Studio


- Select File > New > Project


- Select "Console App(.NET Framework)" option


- Name the project as CalcApp


- Click OK


- Create a class named BasicFormulas


    public class BasicFormulas
    {
        public double Add(double num1, double num2)
        {
            return num1 + num2;
        }

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

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

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

In this application, you can see that there are four basic math functions.


To test the console application, we are going to use Unit Test Project(.NET Framework).


1.PNG

- Right click the solution "CalcApp" then select Add > New Project...


- Select Unit Test Project(.NET Framework)


- Name the unit test project as CalcAppTest then click the "Add" button.


2.PNG

- Delete the UnitTest1 class


3.PNG

- Right click Reference under the project "CalcAppTest" on Solution Explorer. Select the "Add Reference..." option.


4.PNG

- Select the "CalcApp" box from the coming window as shown below.


5.PNG

- Click OK.


- To add a class named BasicFormulasTest, right click the "CalcAppTest" project. Then select Add > New Item...


- Select the option "Class"


- Name BasicFormulasTest.cs


using CalcApp;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace CalcAppTest
{
    [TestClass]
    public class BasicFormulasTest
    {
        [TestMethod]
        public void AddMethod_Test()
        {
            BasicFormulas basicFormulas = new BasicFormulas();
            double res = basicFormulas.Add(20, 10);
            Assert.AreEqual(res, 30);
        }

        [TestMethod]
        public void SubstractMethod_Test()
        {
            BasicFormulas basicFormulas = new BasicFormulas();
            double res = basicFormulas.Substract(5, 2);
            Assert.AreEqual(res, 3);
        }

        [TestMethod]
        public void DivideMethod_Test()
        {
            BasicFormulas basicFormulas = new BasicFormulas();
            double res = basicFormulas.Divide(20, 10);
            Assert.AreEqual(res, 2);
        }

        [TestMethod]
        public void MultiplyMethod_Test()
        {
            BasicFormulas basicFormulas = new BasicFormulas();
            double res = basicFormulas.Multiply(5, 2);
            Assert.AreEqual(res, 10);
        }

    }
}

It is mandatory to define an attribute named [TestClass] at the beginning of the classes for which we write test methods.
With this attribute, the system understands that this class is a test class.


The [TestMethod] attribute must be written at beginning of the test methods. This attribute indicates that method is a test method.


- Select Test > Run > All Tests from the menu as shown below


6.PNG

After writing our test methods, as in the following image, Test Explorer is opened. So you can see the success status.


Output :


7.PNG

Download the source code.