Unit Test In .NET Core Application Using MStest

Introduction

In this article, I am going to explain how you can perform unit tests in a .NET Core application using MStest as test library and C# as the programming language. We will be creating a sample application to test if a given integer is even or not and then, we will be running our unit tests on that.

Prerequisites

  • Install .NET Core 2.0.0 or above SDK from here.
  • Download and install Visual Studio Code from here.
  • Familiarize yourself with Visual Studio Code from here.

Create source project

We will be creating a source project from the terminal window in Visual Studio Code. Open VS code and navigate to view an Integrated Terminal.

 

This will open the terminal window as shown in the image below.

Type the following commands in the terminal window. It will create a directory unit-test-demo and a new solution file for class library and the test project inside that directory.

  • mkdir unit-test-demo
  • cd unit-test-demo
  • dotnet new sln

Navigate to unit-test-demo directory and run these set of commands. It will create our source project TestService.

  • mkdir TestService
  • cd TestService
  • dotnet new classlib


Navigate back to unit-test-demo directory and run the following command. This will add the class library project to the solution.
  1. dotnet sln add TestService/TestService.csproj  

 You can see the success message on the terminal

 

Now, our source project is ready; we will proceed to create our test project.

Create test project

Navigate to unit-test-demo directory and run the following commands.

  • mkdir TestService.Tests
  • cd TestService.Tests
  • dotnet new mstest 


This will create a TestService.Tests directory inside unit-test-demo directory and then create a test project that uses MStest as the test library inside TestService.Tests directory. It will also add MSTest SDK, MSTest test framework and MSTest runner in TestService.Tests.csproj file. But the test project requires other packages to create and run unit tests. So we will add TestService class library as another dependency to the project.

Navigate to TestService.Tests directory and run the following command,
  1. dotnet add reference ../TestService/TestService.csproj  
You can see the following success message in terminal



Now, open the unit-test-demo directory using VS code and open TestService.Tests.csproj file. Here, you can see all the package references.
  1. <Project Sdk="Microsoft.NET.Sdk">  
  2.     <PropertyGroup>  
  3.         <TargetFramework>netcoreapp2.0</TargetFramework>  
  4.         <IsPackable>false</IsPackable>  
  5.     </PropertyGroup>  
  6.     <ItemGroup>  
  7.         <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0-preview-20170628-02" />  
  8.         <PackageReference Include="MSTest.TestAdapter" Version="1.1.18" />  
  9.         <PackageReference Include="MSTest.TestFramework" Version="1.1.18" /> </ItemGroup>  
  10.     <ItemGroup>  
  11.         <ProjectReference Include="..\TestService\TestService.csproj" /> </ItemGroup>  
  12. </Project>  
Now, to add test project to the solution, we will run following command from unit-test-demo directory.
  1. dotnet sln add .\TestService.Tests\TestService.Tests.csproj  

You can observe that the test project has been added succesfully to the solution.



Now, we have successfully created our test project and added it to the solution. Let's proceed to create our first test.

Creating the test

Rename the Class1.cs file in TestService project to TestService.cs.Open TestService.cs file and put following code into it.

  1. using System;  
  2. namespace Test.Services {  
  3.     public class TestService {  
  4.         public bool IsEven(int value) {  
  5.             if (value % 2 == 0) {  
  6.                 return true;  
  7.             } else {  
  8.                 return false;  
  9.             }  
  10.         }  
  11.     }  
  12. }  
Rename UnitTest1.cs in the TestService.Tests project to IsEvenTest.cs and put the following code in it.
  1. using Microsoft.VisualStudio.TestTools.UnitTesting;  
  2. using Test.Services;  
  3. namespace Test.UnitTests.Services {  
  4.     [TestClass]  
  5.     public class TestService_IsEven {  
  6.         private readonly TestService _testService;  
  7.         public TestService_IsEven() {  
  8.                 _testService = new TestService();  
  9.             }  
  10.             [TestMethod]  
  11.         public void ReturnIsEven() {  
  12.             var result = _testService.IsEven(2);  
  13.             Assert.IsTrue(result, "2 is even");  
  14.         }  
  15.     }  
  16. }  

The [TestClass] attribute denotes a class that contains unit tests and the [TestMethod] attribute indicates a method is a test method.

We have created a sample test. To execute it, navigate to TestService.Tests directory and execute the following commands. 
  • dotnet restore
  • dotnet test
 

"dotnet restore" will restore the packages of both the projects and "dotnet test" will build both projects and run all the configured test. As you can see, our configured test has been passed successfully.

You can observe that we have added only one test case (i.e. 2) inside [TestMethod] attribute.If we need to add multiple test cases, then [TestMethod] attribute is not a viable option as we need to write multiple tests having [TestMethod] attribute. So, we will use [DataTestMethod] attribute of MsTest that will enable us to write a suite of similar tests.A [DataTestMethod] attribute represents a suite of tests that execute the same code but have different input arguments. You can use the [DataRow] attribute to specify values for those inputs.

So, we will now write two separate methods in IsEvenTest.cs file, one to test for even numbers and another to test for odd numbers 

  1. [DataTestMethod]  
  2. [DataRow(4)]  
  3. [DataRow(6)]  
  4. [DataRow(8)]  
  5. public void ReturnIsEven(int value) {  
  6.         var result = _testService.IsEven(value);  
  7.         Assert.IsTrue(result, $ "{value} is Even");  
  8.     }  
  9.     [DataTestMethod]  
  10.     [DataRow(5)]  
  11.     [DataRow(7)]  
  12.     [DataRow(9)]  
  13. public void ReturnIsOdd(int value) {  
  14.     var result = _testService.IsEven(value);  
  15.     Assert.IsFalse(result, $ "{value} is Odd");  
  16. }  

Run dotnet test again and you can see that all the 6 test case (3 for even and 3 for odd) has been passed successfully.

 

Conclusion

We have learned how to create and execute a sample unit test project using MStest in .NET Core. Please refer to the attached code for better understanding. Please provide your valuable feedback in the comments below.

Reference

https://docs.microsoft.com/en-gb/dotnet/core/testing/unit-testing-with-mstest 


Similar Articles