Geeks With Blogs

News



Microsoft Store

Support This Site


AddThis Social Bookmark Button

Locations of visitors to this page

Subscribers to this feed

TwitterCounter for @sdorman

Creative Commons License


Scott Dorman Microsoft MVP, Software Architect, Developer, Author

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 .NET (General) , .NET (C#) | Back to top


Comments on this post: Adding custom assertions to MSTest

# re: Adding custom assertions to MSTest
Requesting Gravatar...
Why not use extention methods to place these on the assert object and then you can do it without search-replace?
Left by Mike Hudgell on Jul 01, 2009 8:19 AM

# re: Adding custom assertions to MSTest
Requesting Gravatar...
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?
Left by Eric Swann on Jul 17, 2009 8:54 AM

# re: Adding custom assertions to MSTest
Requesting Gravatar...
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.
Left by Scott on Aug 04, 2009 2:39 PM

# re: Adding custom assertions to MSTest
Requesting Gravatar...
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?
Left by Sarah Vessels on Aug 11, 2009 1:34 PM

# re: Adding custom assertions to MSTest
Requesting Gravatar...
This is great! Many thanks!
Left by Nick on Dec 22, 2011 5:15 AM

Your comment:
 (will show your gravatar)
 


Copyright © Scott Dorman | Powered by: GeeksWithBlogs.net | Join free