Adding custom assertions to MSTest

In my last post I talked about migrating from NUnit to MSTest and mentioned that there were some Assert methods that NUnit provided which did not have corresponding methods in MSTest. For reference, those unavailable Asserts are:

  • Assert.IsNaN
  • Assert.IsEmpty
  • Assert.IsNotEmpty
  • Assert.Greater
  • Assert.GreaterOrEqual
  • Assert.Less
  • Assert.LessOrEqual
  • Assert.IsAssignableFrom
  • Assert.IsNotAssignableFrom
  • CollectionAssert.IsEmpty
  • CollectionAssert.IsNotEmpty
  • StringAssert.AreEqualIgnoringCase
  • StringAssert.IsMatch
  • FileAssert.AreEqual
  • FileAssert.AreNotEqual

Even though MSTest does not provide the same extensibility capabilities as NUnit, it is still possible to add these missing Assert methods by creating your own class library to reference in your unit test project.

In order to help ease any potential issues in migrating from NUnit to MSTest caused by the lack of these Assert methods, I have created a class library that includes all of them except the FileAssert methods and StringAssert.IsMatch. These methods should behave exactly like they do in NUnit. (In fact, the unit tests are the same ones used in NUnit, with only a few minor differences.)

The following table shows the changes you will need to make in order to use the new asserts. As you can see, the changes can be easily made using search and replace.

NUnitMSTest Extension Library
Assert.IsNaN ConditionAssert.IsNaN
Assert.IsEmpty CustomAssert.IsEmpty
Assert.IsNotEmpty CustomAssert.IsNotEmpty
Assert.Greater ConditionAssert.Greater
Assert.GreaterOrEqual ConditionAssert.GreaterOrEqual
Assert.Less ConditionAssert.Less
Assert.LessOrEqual ConditionAssert.LessOrEqual
Assert.IsAssignableFrom TypeAssert.IsAssignableFrom
Assert.IsNotAssignableFrom TypeAssert.IsNotAssignableFrom
CollectionAssert.IsEmpty CustomAssert.IsEmpty
CollectionAssert.IsNotEmpty CustomAssert.IsNotEmpty
StringAssert.AreEqualIgnoringCase CustomAssert.IsNotEmpty

I also added the ExceptionAssert class from my post talking about the dangers of using the ExpectedException attribute.

You can the class from my SkyDrive public folder.

DotNetKicks Image

This article is part of the GWB Archives. Original Author: Scott Dorman

New on Geeks with Blogs

  • We Won The One Award I Actually Care About

    Full Scale made the Inc. 5000 for the fifth year straight, the 12th listing across my three companies. Here is why the one award you cannot buy is worth stopping for.

  • Your Customers Build the Features Now

    I let a tool I liked sit dead for a year rather than build the features I wanted. An MCP server meant I never had to, and your customers can do the same to your product.

  • Get the Size of a Directory in Linux the Easy Way

    du -sh for the quick answer, ncdu for the cleanup, df for the disk itself: every command for checking directory size in Linux, plus why du and df never agree.

  • Vim Search and Replace: The Ultimate Guide

    One :%s command replaces every match in a file before a find dialog would even open. The Vim substitute patterns worth the muscle memory: flags, ranges, capture groups, and multi-file edits.