Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Using MSTest for Unit Testing in C#

Introduction

MSTest is a testing framework developed by Microsoft that is used for unit testing in .NET applications. It is fully integrated with Visual Studio and provides a straightforward way to test your code. This tutorial will guide you through the process of using MSTest, from setting up your project to writing and running tests.

Setting Up Your Project

To get started with MSTest, you need to create a test project in Visual Studio. Follow these steps:

  1. Open Visual Studio.
  2. Go to File > New > Project.
  3. Select Unit Test Project (.NET Core).
  4. Give your project a name and click Create.

Once your project is created, you should see a default test class named UnitTest1.cs.

Writing Your First Test

Let's write a simple test to check if a method returns the correct value. Consider the following method in a class named Calculator:

public class Calculator
{
    public int Add(int a, int b)
    {
        return a + b;
    }
}

We want to test the Add method to ensure it returns the correct sum of two integers. Here is how you can write a test for this method:

using Microsoft.VisualStudio.TestTools.UnitTesting;

[TestClass]
public class CalculatorTests
{
    [TestMethod]
    public void Add_ShouldReturnCorrectSum()
    {
        // Arrange
        var calculator = new Calculator();
        int a = 5;
        int b = 3;
        int expected = 8;

        // Act
        int result = calculator.Add(a, b);

        // Assert
        Assert.AreEqual(expected, result);
    }
}

In this example, we are testing the Add method of the Calculator class. The [TestMethod] attribute indicates that the method is a test method.

Running Your Tests

To run your tests, follow these steps:

  1. Build your project by clicking on Build > Build Solution.
  2. Open the Test Explorer window by going to Test > Test Explorer.
  3. Click on the Run All button in the Test Explorer.

If the test passes, you will see a green check mark next to the test method. If it fails, a red cross will indicate the failure.

Advanced Features

Data-Driven Tests

MSTest supports data-driven tests, which allow you to run a test method multiple times with different data. Here's an example:

using Microsoft.VisualStudio.TestTools.UnitTesting;

[TestClass]
public class CalculatorTests
{
    [DataTestMethod]
    [DataRow(5, 3, 8)]
    [DataRow(-1, -1, -2)]
    [DataRow(0, 0, 0)]
    public void Add_ShouldReturnCorrectSum(int a, int b, int expected)
    {
        // Arrange
        var calculator = new Calculator();

        // Act
        int result = calculator.Add(a, b);

        // Assert
        Assert.AreEqual(expected, result);
    }
}

In this example, the [DataTestMethod] attribute is used to indicate a data-driven test, and the [DataRow] attribute specifies the set of data for each test run.

Conclusion

In this tutorial, we have covered the basics of using MSTest for unit testing in C#. We started with setting up a test project, writing a simple test, running tests, and briefly touched on advanced features like data-driven tests. MSTest is a powerful framework that can help you ensure the reliability and correctness of your code.