ExceptionException Attribute message argument ignored

Correct me if I’m wrong here, but I have reason to believe that the “message” argument on the ExceptionExceptionAttribute constructor is ignored.

Here’s my proof:

I have a class I’m testing:

Public Class Class1

Public Sub Foo()

Throw New ApplicationException("kaboom!")

End Sub

End Class

And a unit test class:

Imports Microsoft.VisualStudio.TestTools.UnitTesting

Imports ClassLibrary1

<TestClass()> _

Public Class Class1Tests

<TestMethod()> _

Public Sub FooTest()

Dim target As Class1 = New Class1

target.Foo()

End Sub

<TestMethod()> _

<ExpectedException(GetType(ApplicationException))> _

Public Sub FooTest2()

Dim target As Class1 = New Class1

target.Foo()

End Sub

<TestMethod()> _

<ExpectedException(GetType(ApplicationException), "kaboom!")> _

Public Sub FooTest3()

Dim target As Class1 = New Class1

target.Foo()

End Sub

<TestMethod()> _

<ExpectedException(GetType(ApplicationException), "blammo!")> _

Public Sub FooTest4()

Dim target As Class1 = New Class1

target.Foo()

End Sub

End Class

When I run the tests, I get these results:

Failed FooTest TestProject1 Test method TestProject1.Class1Tests.FooTest threw exception: System.ApplicationException: kaboom!.

Passed FooTest2 TestProject1

Passed FooTest3 TestProject1

Passed FooTest4 TestProject1

The test FooTest4 should be failing.

This could be a moderate issue for people who rely on the message argument for tests that throw the same exception type but with different messages for different failure conditions. They will get false positives.



Answer this question

ExceptionException Attribute message argument ignored

  • Dave Holmes

    Hello!

    The "message" parameter on the ExpectedException is not the text to expect from the exception. It is actually the message to display when the test fails, same as the message in Assert.Fail().

    We don't provide a way to construct ExpectedException attribute with expected Exception.Message. One of the reasons for that is usually exception messages are localized strings that take parametrs (like "Operation {0} failed") and the attribute can take only a constant expression.

    Thank you,
    Michael Koltachev,
    VSTS


  • Alan Hoiland

    This is actually a known issue. The message parameter should appear in the result details if the wrong exception is thrown, but there's a problem with reading it out of the attribute from within the Visual Studio UI. If you run the test from the command line with MSTest, you should see the message as expected. This will be fixed in future releases.
  • luke_5290

    It does not seem to do that either. The only error message that is available in the TestResults is <Test method X did not throw the expected exception>.

    I get this message even if I pass in a string as the second argument of the ExpectedException constructor. I checked the raw result file to see if the string may be burried somewhere in the XML and just not surface in the TestResults View in Visual Studio, but I could not find it anywhere.

    My unit test is written in VB.


  • ExceptionException Attribute message argument ignored