Practical 11: Testing with MSTest

CAB201 - Programming Principles

School of Computer Science, Faculty of Science

MSTest

MSTest is a testing framework for the .NET platform (default in Visual Studio). It makes use of Attributes to define test methods and test classes.

[TestClass]
public class Adder
{
    [TestMethod]
    public void TestAddPositiveIntegers()
    {
        int result = Adder.Add(1, 1);
        int expected = 2;
        Assert.AreEqual(expected, result);
    }
}
CAB201 - Programming Principles
School of Computer Science, Faculty of Science

MSTest - Visual Studio (Documentation)

To set up a test project in Visual Studio:

  1. In Solution Explorer, right-click the solution and select Add > New Project.
  2. Search for Test in the search box, and select MSTest Test Project.
  3. Click Next and choose the project name and location, and click Create.
  4. Right-click the test project and select Add > Project Reference.
  5. Select the project you want to test and click OK.
  6. Add using YourProjectNameSpace; to the test class to access its classes and methods.

To run the tests, right-click the test project and select Run Tests.

CAB201 - Programming Principles
School of Computer Science, Faculty of Science

MSTest - Visual Studio Code (Documentation)

Setting up a test project in Visual Studio Code is more manual:

  1. Open the terminal in the parent folder of your project folder.
  2. Create a new test project with the command dotnet new mstest -o YourTestProjectName.
  3. Add a reference to the project you want to test with the command dotnet add YourTestProjectName reference YourProjectName.

To run the tests, navigate to the test project folder and run the command dotnet test.

CAB201 - Programming Principles
School of Computer Science, Faculty of Science

MSTest - Attributes

Attribute Description
[TestClass] Indicates that the class contains test methods.
[TestMethod] Indicates that the method is a test method.
[TestInitialize] Indicates that the method will be run before each test method.
[TestCategory("CategoryName")] Indicates that the test method belongs to a category. Multiple categories can be added.
[ExpectedException(typeof(Exception))] Indicates that the test method is expected to throw an exception.
CAB201 - Programming Principles
School of Computer Science, Faculty of Science

MSTest - Assert Methods

Common Assert methods:

Method Description
AreEqual(expected, actual) / AreNotEqual(expected, actual) Verifies that two values are equal / not equal.
IsTrue(condition) / IsFalse(condition) Verifies that the condition is true / false.
IsNotNull(object) / IsNull(object) Verifies that the object is not null / null.
IsInstanceOfType(object, type) Verifies that the object is an instance of the specified type.
CAB201 - Programming Principles
School of Computer Science, Faculty of Science

Testing and Documentation

An API Reference is a document that describes the methods and properties of a class. It is often useful to use it as a reference for black-box testing.

Example: https://cab201.github.io/docs/api/Totaliser.Totaliser.html documents the following class:

public class Totaliser
{
    public int Sum { get; }
    public void EnterValue(int value) { /* Implementation */ }
    public void Reset() { /* Implementation */ }
}
CAB201 - Programming Principles
School of Computer Science, Faculty of Science