Scott Dorman

ephemeral segment

  Home  |   Contact  |   Syndication    |   Login
  608 Posts | 11 Stories | 900 Comments | 51 Trackbacks

News


Post Categories

Image Galleries


Microsoft Store


Creative Commons License



Locations of visitors to this page

Subscribers to this feed

TwitterCounter for @sdorman

View blog authority

Add to Technorati Favorites

Windows Live Alerts

AddThis Social Bookmark Button

LinkedIn profile

Community Credit profile

The Code Project

Follow me on Twitter

Get Free Shots from Snap.com

Community Credit Hall of Fame

Get Feedghost

Xobni outlook add-in for your inbox



Support This Site

Tag Cloud


Article Categories

Archives

Post Categories

Image Galleries

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.

NUnit MSTest 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 download the class from my SkyDrive public folder.

Technorati Tags: , ,
DotNetKicks Image
posted on Saturday, January 31, 2009 9:10 PM

Feedback

# re: Adding custom assertions to MSTest 7/1/2009 8:19 AM Mike Hudgell
Why not use extention methods to place these on the assert object and then you can do it without search-replace?

# re: Adding custom assertions to MSTest 7/17/2009 8:54 AM Eric Swann
Hello Scott, I'm trying to convert and I have a lot of developers that use the NUnit: Assert.That(x, Is.GreaterThan(y)) approach. Have you seen any constructs like this for MSTest?

# re: Adding custom assertions to MSTest 8/4/2009 2:39 PM Scott
Mike, you can't use extension methods as the Assert class itself is static so there is no instance which can be extended.

Eric, I have not seen that syntax for MSTest, although it would not be difficult to add that type of syntax to the class library I mention in the post. I think it would be time consuming to add and verify, but it is possible to do.

# re: Adding custom assertions to MSTest 8/11/2009 1:34 PM Sarah Vessels
Thanks for this! I was wanting to add static extension methods to the MS Assert class to have methods like IsEmpty, but that's impossible with the current C#, and that's how I discovered this. I'm checking out the source now, and I see "Copyright (C) 2001-2009 Campari Software. All rights reserved." What is the license for using this code?

# re: Adding custom assertions to MSTest 12/22/2011 5:15 AM Nick
This is great! Many thanks!

Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: