MSTest in C#–A Beginner’s Guide

Unit testing is a core component of software development that ensures individual pieces of your code (units) function as expected. MSTest is one of the most widely-used testing frameworks in the .NET projects. This post takes you through an interactive experience building a sample solution step-by-step to learn unit testing concepts using MSTest.

Introduction to MSTest in C#–A Beginner’s Guide

Getting Started

MSTest offers a straightforward and user-friendly framework for writing unit tests in C# and other .NET languages, enabling developers to test individual methods, classes, or complete applications.

What is MSTest?

MSTest is a testing framework developed by Microsoft that allows developers to write and run unit tests for .NET applications. With MSTest, developers can easily create automated tests for their code, run them frequently, and get feedback on any issues that are found.

It is part of the Microsoft.NET.Test.Sdk and is used to ensure the logic in your methods and classes works as expected by writing test methods decorated with attributes like [TestMethod] and [TestClass].

Create MSTest Project

Here's a step-by-step guide to create a project using MSTest in C# with Visual Studio:
  1. Open Visual Studio
  2. Go to File > New > Project
  3. Search for "MSTest Test Project"
    • Select "MSTest Test Project (.NET Core)" or ".NET Framework", depending on your needs
  4. Click Next
  5. Configure the Project
    • Project Name: MyMSTests
    • Location: Choose a folder
    • Solution:
    • You can add it to an existing solution or create a new one
    • Click Create

Install MSTest Packages

Ensure you have the following NuGet packages installed In your test project, if not install these packages:
 dotnet add package MSTest.TestAdapter  
 dotnet add package MSTest.TestFramework  

MS Test Schenario

Lets say we have a class named Calculator with functions called Add, Subst, Divi and Mulifi to perform addion, substraction, division, and Mulification. We want to perform a unit test using MSTest framework.

Calculator.cs
 public class Calculator  
 {  
   public int Add(int a, int b)  
   { return a + b; }  
   public int Subst(int a, int b)  
   { return a - b; }  
   public int Mulifi(int a, int b)  
   { return a * b; }  
   public int Divi(int a, int b)  
   { return a / b; }  
 }  

Writing MSTest Unit Tests in C#

Writing unit tests in C# involves using the Microsoft MSTest framework to write, organize, and run tests for your .NET code. Below is a quick guide to help you get started.

Example:- Basic
 namespace myMSTest  
 {  
   [TestClass] // Marks this class as a test class  
   public sealed class CalculatorTest  
   {  
     [TestMethod]// Marks this method as a test  
     public void TestAdd()  
     {  
       // Arrange  
       var calculator = new Calculator();  
       // Act
       var result = calculator.Add(2, 3);
       // Assert
       Assert.AreEqual(5, result);
     }  
     [TestMethod]  
     public void TestSub()  
     {  
       var calculator = new Calculator();  
        var result = calculator.Sub(5, 3);
 		Assert.AreEqual(2, result);  
     }  
     [TestMethod]  
     public void TestDiv()
    {
        var result = calculator.Div(10, 2);
        Assert.AreEqual(5, result);
    }  
     [TestMethod] 
     public void TestMul()
    {
        var result = calculator.Mul(10, 2);
        Assert.AreEqual(20, result);
    } 
   }  

Example:- Using TestInitialize
 namespace myMSTest  
 {  
   [TestClass] // Marks this class as a test class  
   public sealed class CalculatorTest  
   {  
     private Calculator calculator;  
     [TestInitialize]  
     public void Setup()  
     {  
       calculator = new Calculator();  
     }  
     [TestMethod]// Marks this method as a test  
     public void TestAdd()  
     {  
       // Act  
       var result = calculator.Add(2, 3);  
       // Assert  
       Assert.AreEqual(5, result);  
     }  
     [TestMethod]  
     public void TestSub()  
     {  
       var result = calculator.Sub(5, 3);  
       Assert.AreEqual(2, result);  
     }  
     [TestMethod]  
     public void TestDiv()  
     {  
       var result = calculator.Div(10, 2);  
       Assert.AreEqual(5, result);  
     }  
     [TestMethod]  
     public void TestMul()  
     {  
       var result = calculator.Mul(10, 2);  
       Assert.AreEqual(20, result);  
     }  
   }  

Example:- Data-Driven Testing
 namespace myMSTest  
 {  
   [TestClass] // Marks this class as a test class  
   public sealed class CalculatorTest  
   {  
     [TestMethod]// Marks this method as a test  
     [DataRow(1, 2, 3)]  
     [DataRow(-1, -1, -2)]  
     [DataRow(0, 0, 0)]  
     public void TestAdd(int a,int b, int expested)  
     {  
       // Arrange  
       var calculator = new Calculator();  
       // Act  
       var result = calculator.Add(a, b);  
       // Assert  
       Assert.AreEqual(expested, result);  
     }  
     [TestMethod]  
     [DataRow(1, 2, 3)]  
     [DataRow(-1, -1, -2)]  
     [DataRow(0, 0, 0)]  
     public void TestSub(int a, int b, int expested)  
     {  
       var calculator = new Calculator();  
       var result = calculator.Sub(a, b);  
       Assert.AreEqual(expested, result);  
     }  
     [TestMethod]  
     [ExpectedException(typeof(DivideByZeroException))]  
     [DataRow(1, 2, 3)]  
     [DataRow(-1, -1, -2)]  
     [DataRow(0, 0, 0)]  
     public void TestDiv(int a, int b, int expested)  
     {  
       var calculator = new Calculator();  
       var result = calculator.Div(a, b);  
       Assert.AreEqual(expested, result);  
     }  
     [TestMethod]// Marks this method as a test  
     [DataRow(1, 2, 3)]  
     [DataRow(-1, -1, -2)]  
     [DataRow(0, 0, 0)]  
     public void TestMul(int a, int b, int expested)  
     {  
       var calculator = new Calculator();  
       var result = calculator.Mul(a, b);  
       Assert.AreEqual(expested, result);  
     }  
   }  

Example:- Exceptions Testing
 [TestMethod]  
 [ExpectedException(typeof(DivideByZeroException))]  
 public void TestDiv()  
 {  
   var calculator = new Calculator();  
   calculator.Div(10, 0); // should throw  
 }  

Running Tests

  • Visual Studio: Use Test Explorer (TestRun All Tests)
  • CLI:
     dotnet test  
    

Common MSTest Attributes

  1. [TestClass]: Marks a class as a test class.
  2. [TestMethod]: Marks a method as a test.
  3. [TestInitialize]: Runs before each test.
  4. [TestCleanup]: Runs after each test.
  5. [ClassInitialize]: Runs once before all tests in a class.
  6. [ClassCleanup]: Runs once after all tests in a class.
  7. [DataRow(...)]: Provides data for a [DataTestMethod].
  8. [DataTestMethod]: Allows parameterized tests.
  9. [ExpectedException]: Asserts a method throws a specific exception.

Summary

MSTest is a powerful and beginner-friendly framework for unit testing in C#. Its seamless integration with Visual Studio, straightforward syntax, and official support make it a solid choice for testing .NET applications.

Thanks

Kailash Chandra Behera

I am an IT professional with over 13 years of experience in the full software development life cycle for Windows, services, and web-based applications using Microsoft .NET technologies.

Previous Post Next Post

نموذج الاتصال