Test Assertions With MsTest (Unit Tests)

As you know in MsTest [ExpectedException(typeof(ApplicationException), “error msg”)] doesn’t verify the error message, which is thrown by an exception. Therefore no matter what exception message it throws, the test will pass
Here is the easy way to verify this exception message.
In this example “HelloWorld” application throws two argument exception for empty and null parameters.
HelloWorld
To extend the Ms TestIt is required to import  MSTestExtensions through Nuget as below to the target project prior  to use ‘MSTestExtensions’. Please use below steps;1. Right Click on project reference and select Nuget packages
Nuget
Then search for ‘MSTestExtensions’ and installed it (see below)
MSExtention
Refactor code as follows;
Then write unit test as follows using ExceptionAssert (This provides by MSTestExtensions ) to verify empty string with correct exception and the exception message (this displays to user)
Test 1 : (Empty String)
EmptyString
Test 2 : (Null String)
NullString
3. Test3 : (With in Constructor)
Constructor
I hope this helps. Thanks
Advertisements

3 responses to “Test Assertions With MsTest (Unit Tests)

  • sergeromero

    Thank you for posting this. I installed it and unfortunately this is a very limited solution since it only supports Actions which means you cannot assert the exception’s message in a constructor or in a function.

    I apologize for the shameless self promotion. I wrote a post in which there is a link to an attribute that asserts the exception’s message and I also add some code that follows the approach of this extension but it can accept Actions, Functions and constructors with any number of parameters and types.

    http://sergeromerosoftware.wordpress.com/2013/04/08/asserting-the-expected-exception-message/

    • trendvirtual

      Hi, Thank you for your comment on this. I have gone through your post and it is excellent. Generally I use MsTest for test automation suites (Which is written in C# CodedUI). In terms of automation testing this provides enough coverage. Actually I didn’t have enough time to apply this other areas such constructors, functions etc; Having said that many internal developers suggested me to use xUnit instead of MsUnit 

      Anyway as you mentioned, I changed my unit test to verify exception message in a constructor. This works fine for me. As an example

      ExceptionAssert.Throws
      (() => new HelloWorld(null), “Invalid Name, Name can’t be null”);

      Is this what you expect?

      I found this post is very interesting too. This shows how to build your own extension method to verify exception message

      http://mariuszwojcik.wordpress.com/2011/04/16/mstest-expectedexceptionwithmessageattribute/

  • sergeromero

    Thank you for your compliment on my post. Hopefully it can be useful to make your library more robust and I wish that lots of developers use it to improve their testing.

    I may have done something wrong when I tried your code because I was not able to make it compile when I tried to test the exception being thrown on a constructor, I will take a look again.

    Weather you use xUnit or MsUnit, the way I see it is a matter of personal choice or whatever your team is using. The important thing to consider there is that there should be consistency so your team should get together, decide between one or the other and then everybody should use the same.

    Good luck and keep improving your library!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: