Creating and Running Unit Tests for a .NET Core Application

Unit testing has become a major part in the daily life of a developer these days and most of them are expected to write unit tests when they add a new functionality in the software that is under development. There are lot of third party tools like NUnit, xUnit as well as MS Test framework for writing unit tests for applications developed in .NET. In this post, I am going to detail the steps needed for adding unit test in .NET Core app using Microsoft Test Framework.

Creating Project

Let's create a simple .NET Core console application project and then a unit test project for running the tests. For that I have created two folders, src and test for each project and added these in the projects key in global.json file in the root directory. 

{
"projects":["src", "test"]
}

After that, I created the console application using the dotnet new command and the contents of the Program.cs file and project.json is given below.

Program.cs

Created a new class and added a new method which returns the sum of the values in the two variables passed into it.

project.json

Let's compile and run the application from the command prompt using dotnet run command. Before that make sure that all your dependencies are properly restored using the dotnet restore command as shown below. In this case, the compilation is done using the dotnet build command, you can also use dotnet run command to do the compilation which then subsequently executes the application upon sucessful build.

Creating Test Project 

We have implemented the functionality in the application and let's see how we can write unit test for that. First, we need to add the references for Microsoft Test Framework in the project.json file as shown in the below snippet. After adding the references, make sure that you have set the testrunner property as mstest and also added the reference to the project which we have create above.

project.json

Now, we will create a class file for writing the unit tests targeting the method in Program.cs class. For this, I am going to create the file under the test directory. Make sure to decorate the class with [TestClass] attribute and unit test methods with [TestMethod] attribute.

ProgramUnitTest.cs

So the structure of our solution will be like the one in the image given below. It's always a good approach to seggregate the functionalities into different folders like all your source code is saved inside a folder and all the unit test cases is created under another folder.

To execute the test methods, first we need to restore the files needed for MS Test framework using the dotnet restore commnad

   

Once the restore is completed, build the unit test project using the dotnet build command and then use dotnet test command to execute all the test methods specified in that file.

 

When the command is executed, it will identify and execute the methods which has the TestMethod attribute and will show the summary of the results as shown above.


No Comments

Add a Comment