Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assert exception from NUnit to MS TEST

Tags:

mstest

nunit

I have some tests where i am checking for parameter name in exception. How do i write this in MS TEST?

ArgumentNullException exception = 
              Assert.Throws<ArgumentNullException>(
                            () => new NHibernateLawbaseCaseDataLoader( 
                                               null, 
                                               _mockExRepository,
                                               _mockBenRepository));

Assert.AreEqual("lawbaseFixedContactRepository", exception.ParamName);

I have been hoping for neater way so i can avoid using try catch block in the tests.

like image 291
cpoDesign Avatar asked Nov 21 '11 14:11

cpoDesign


People also ask

How do I assert exceptions in NUnit?

It's also in a class by itself in that it returns an Exception, rather than void, if the Assert is successful. See the example below for a few ways to use this. Assert. Throws may be used with a constraint argument, which is applied to the actual exception thrown, or with the Type of exception expected.

How do I assert exceptions in MSTest?

There are some complexities to this which will come in another blog post, but the result is we can now use the following syntax to assert an exception in MSTest: Assert. Throws(() => sc. Add("-1"));

How do I use assert in NUnit?

Apply a constraint to an actual value, succeeding if the constraint is satisfied and throwing an assertion exception on failure. Asserts that a condition is true. If the condition is false the method throws an AssertionException. Asserts that a condition is true.

How do I raise an exception in C#?

You can explicitly throw an exception using the C# throw or the Visual Basic Throw statement. You can also throw a caught exception again using the throw statement. It is good coding practice to add information to an exception that is re-thrown to provide more information when debugging.


2 Answers

public static class ExceptionAssert
{
  public static T Throws<T>(Action action) where T : Exception
  {
    try
    {
      action();
    }
    catch (T ex)
    {
      return ex;
    }

    Assert.Fail("Expected exception of type {0}.", typeof(T));

    return null;
  }
}

You can use the extension method above as a test helper. Here is an example of how to use it:

// test method
var exception = ExceptionAssert.Throws<ArgumentNullException>(
              () => organizations.GetOrganization());
Assert.AreEqual("lawbaseFixedContactRepository", exception.ParamName);
like image 154
Dan Avatar answered Oct 22 '22 21:10

Dan


Shameless plug, but I wrote a simple assembly that makes asserting exceptions and exception messages a little easier and more readable in MSTest using Assert.Throws() syntax in the style of nUnit/xUnit.

You can download the package from Nuget using: PM> Install-Package MSTestExtensions

Or you can see the full source code here: https://github.com/bbraithwaite/MSTestExtensions

High level instructions, download the assembly and inherit from BaseTest and you can use the Assert.Throws() syntax.

The main method for the Throws implementation looks as follows:

public static void Throws<T>(Action task, string expectedMessage, ExceptionMessageCompareOptions options) where T : Exception
{
    try
    {
        task();
    }
    catch (Exception ex)
    {
        AssertExceptionType<T>(ex);
        AssertExceptionMessage(ex, expectedMessage, options);
        return;
    }

    if (typeof(T).Equals(new Exception().GetType()))
    {
        Assert.Fail("Expected exception but no exception was thrown.");
    }
    else
    {
        Assert.Fail(string.Format("Expected exception of type {0} but no exception was thrown.", typeof(T)));
    }
}

More info here.

like image 35
Bradley Braithwaite Avatar answered Oct 22 '22 22:10

Bradley Braithwaite