Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Unit Test in .NET Core Application Using MStest

DZone's Guide to

Unit Test in .NET Core Application Using MStest

Learn how to perform unit tests in a .NET Core application in C#, using the test library MStest, using a simple demo application.

· DevOps Zone ·
Free Resource

Do you need to strengthen the security of the mobile apps you build? Discover more than 50 secure mobile development coding practices to make your apps more secure.

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.

Before proceeding further, I would recommend downloading the source code from GitHub.

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, a new solution file for the class library, and the test project inside that directory.

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

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

  • mkdir TestService
  • cd TestService
  • dotnet new classlib

Navigate back to the unit-test-demo directory and run the following command. This will add the class library project to the solution.

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 the unit-test-demo directory and then create a test project that uses MStest as the test library inside the TestService.Tests directory. It will also add the MSTest SDK, MSTest test framework and MSTest runner in the TestService.Tests.csproj file. 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,

dotnet add reference ../TestService/TestService.csproj

You can see the following success message in the terminal:

Now, open the unit-test-demo directory using VS code and open the TestService.Tests.csproj file. Here, you can see all the package references.

<Project Sdk="Microsoft.NET.Sdk">  
    <PropertyGroup>  
        <TargetFramework>netcoreapp2.0</TargetFramework>  
        <IsPackable>false</IsPackable>  
    </PropertyGroup>  
    <ItemGroup>  
        <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0-preview-20170628-02" />  
        <PackageReference Include="MSTest.TestAdapter" Version="1.1.18" />  
        <PackageReference Include="MSTest.TestFramework" Version="1.1.18" /> </ItemGroup>  
    <ItemGroup>  
        <ProjectReference Include="..\TestService\TestService.csproj" /> </ItemGroup>  
</Project>

Now, to add the test project to the solution, we will run the following command from the unit-test-demo directory.

dotnet sln add .\TestService.Tests\TestService.Tests.csproj

You can observe that the test project has been added successfully 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 the TestService project to TestService.cs. Open the TestService.cs file and put the following code into it:

using System;  
namespace Test.Services {  
    public class TestService {  
        public bool IsEven(int value) {  
            if (value % 2 == 0) {  
                return true;  
            } else {  
                return false;  
            }  
        }  
    }  
}

Rename UnitTest1.cs in the TestService.Tests project to IsEvenTest.cs and put the following code in it:

using Microsoft.VisualStudio.TestTools.UnitTesting;  
using Test.Services;  
namespace Test.UnitTests.Services {  
    [TestClass]  
    public class TestService_IsEven {  
        private readonly TestService _testService;  
        public TestService_IsEven() {  
                _testService = new TestService();  
            }  
            [TestMethod]  
        public void ReturnIsEven() {  
            var result = _testService.IsEven(2);  
            Assert.IsTrue(result, "2 is even");  
        }  
    }  
}

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

We have created a sample test. To execute it, navigate to the 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 the [TestMethod] attribute.If we need to add multiple test cases, then the [TestMethod] attribute is not a viable option as we need to write multiple tests that have the [TestMethod] attribute. So, we will use the [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 the IsEvenTest.cs file, one to test for even numbers and another to test for odd numbers

[DataTestMethod]  
[DataRow(4)]  
[DataRow(6)]  
[DataRow(8)]  
public void ReturnIsEven(int value) {  
        var result = _testService.IsEven(value);  
        Assert.IsTrue(result, $ "{value} is Even");  
    }  
    [DataTestMethod]  
    [DataRow(5)]  
    [DataRow(7)]  
    [DataRow(9)]  
public void ReturnIsOdd(int value) {  
    var result = _testService.IsEven(value);  
    Assert.IsFalse(result, $ "{value} is Odd");  
}

Run dotnet test again and you can see that all the test cases (3 for even and 3 for odd) have 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.

Download the source code from GitHub.

Check out tips for blazing the way from agile to DevSecOps with security built into your mobile app toolchain.

Topics:
.net core ,mstest ,c# ,visual studio ,unit testing ,devops

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}