C# Unit Test Example - MSTest - Visual Studio


A unit test is a function that tests a unit of work.

MSTest is a unit testing framework.

MSTest is integrated with Visual Studio.

Creating The First Unit Test Example

Add a Unit Test Project to the solution.
  1. Right click on the solution.
  2. Add a new project.
  3. Under Visual C#, select Unit Test Project in the Test category.
Decorate the class that contains test methods with TestClass attribute.
Decorate the Test Method with TestMethod attribute.

To write the unit test we follow the AAA pattern.
  • Arrange : Initializes objects and sets the value of the data that is passed to the method being tested.
  • Act : Invokes the method being tested.
  • Assert : Verifies that the method being tested behaves as expected.
assert - state a fact or belief confidently and forcefully

Run The Unit Test

  1. Select the TEST menu.
  2. Select windows.
  3. Select Test Explorer.
  4. Within the Test Explorer we can see all the unit tests.
  5. Right click on an unit test and select Run Selected Tests.
  6. Check the test result.

Unit Test Naming Convention

[UnitOfWork_StateUnderTest_ExpectedBehavior]

  • Unit of work : the name of the method being tested
  • State under test : the input values for the method
  • Expected behavior : what the method returns for the specified input


Unit Test Example - The Method Being Tested

namespace MyClassLibrary
{
    public class Calculator
    {
        public int Divide(int numerator, int denominator)
        {
            int result = numerator / denominator;
            return result;
        }
    }
}

Unit Test Example - The Test Method And Test Class

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace MyClassLibrary.Tests
{
    [TestClass]
    public class CalculatorTests
    {
        [TestMethod]
        public void Divide_PositiveNumbers_ReturnPositiveQuotient()
        {
            // Arrange : Initializes objects and sets the value of the data that is passed to the method being tested.
            int expected = 5;
            int numerator = 20;
            int denominator = 4;

            // Act : Invokes the method being tested.
            MyClassLibrary.Calculator calculator = new MyClassLibrary.Calculator();
            int actual = calculator.Divide(numerator, denominator);

            // Assert : Verifies that the method being tested behaves as expected.
            Assert.AreEqual(expected, actual);
        }

        [TestMethod]
        public void Divide_NegativeNumbers_ReturnPositiveQuotient()
        {
            // Arrange : Initializes objects and sets the value of the data that is passed to the method being tested.
            int expected = 5;
            int numerator = -20;
            int denominator = -4;

            // Act : Invokes the method being tested.
            MyClassLibrary.Calculator calculator = new MyClassLibrary.Calculator();
            int actual = calculator.Divide(numerator, denominator);

            // Assert : Verifies that the method being tested behaves as expected.
            Assert.AreEqual(expected, actual);
        }
    }
}


MSTest Unit Testing Tutorial for beginners

What is a Unit Test

Creating your first unit test

Benefits of unit testing

Unit testing and continuous integration

Unit test naming convention

張貼留言

0 留言