ExpectedException might not be what you've expected

If you've switched from NUnit to the VSTS Unit Testing then I am pretty sure you've used the ExpectedException attribute. If you haven't it looks something like this:

[ExpectedException( typeof( ApplicationException ), "Message" )]

Using it with a test would look like the following:

[TestMethod, ExpectedException( typeof ( ApplicationException ) ) ]
public void ut1()
{
throw new ApplicationException();
}

[TestMethod, ExpectedException( typeof( ApplicationException ), "foo" )]
public void ut2()
{
throw new ApplicationException("bar");
}

You would naturally expect test ut1 to pass and ut2 to fail. ut2 should fail on the bases that the message contained within the exception was in fact different. Not true. Both actually pass and not because of a bug but rather a new feature introduced to the attribute. The Message property is actually a message that prints out with the test when it fails; just like the optional message property you find on all of the Assert methods.

Assert.IsTrue( false == true, "Developer put explanation if this test was to fail" );

That message will print with the output of the test like so:

Error 1 TestCase 'ExpectedExceptionTest.ExpectedException.ut3' failed: Assert.IsTrue failed. Developer put explanation if this test was to fail
at ExpectedExceptionTest.ExpectedException.ut3() in C:\MyPrograms\ExpectedException\ExpectedExceptionTest\ExpectedException.cs:line 26 C:\MyPrograms\ExpectedException\ExpectedExceptionTest\ExpectedException.cs 26

Lets correct ut2:

[TestMethod, ExpectedException( typeof( ApplicationException ), "I failed because I am looking for the wrong excpeption type" )]
public void newUt2()
{
throw new Exception( "foobar" );
}

This will in fact fail because the Exception type thrown was different. When it fails we will see that message in the output.

Having said all that, there is in fact a bug. When you run a test with that ExpectedException attribute in Visual Studio you will not see the message on the failure output; if you run it in MSTest.exe you will.

ExpectedException.cs (.75 KB)