1

I created a custom attribute for unit testing based ExpectedExceptionBaseAttribute:

public class ExpectedEngineExceptionAttribute : ExpectedExceptionBaseAttribute

I override the Verify method as follow:

    protected override void Verify(Exception exception)
    {
        RethrowIfAssertException(exception);
        throw new Exception("Hola!!");
    }

Here's the TestMethod (It calls a method that throws a ):

    [TestMethod]
    [ExpectedEngineExceptionAttribute()]
    public void MyTest()
    {
        SomeCode();
    }

The output in the unit testing window just shows:

Exception has been thrown by the target of an invocation.

I would except to see the message Hola!!

When I run the following Test Method:

    [TestMethod]
    [ExpectedException(typeof(EngineException))]
    public void MyTest2()
    {
        SomeCode();
    }

The output is :

Test method NotificationInputAttributeInvalidTypeTest threw exception System.Exception, but exception EngineException was expected. Exception message: System.Exception: SS

What am I missing to have my "Hola" exception message to show in the output window?

I decompiled the ExpectedExceptionAttribute and did what it does in my custom attribute but it's not working...

UPDATE: Adding a breakpoint confirms the Exception("Hola!!") is thrown:

enter image description here

3
  • I suggest not to use an attribute to define your test assertions. Try to follow the "AAA" style and make the assertion explicitly in the C# code. This will make it easier to debug it, too. See also: darkroastjava.wordpress.com/2014/01/21/… Commented Sep 17, 2014 at 14:59
  • Thanks for the suggestion but I need to use an attribute. Commented Sep 17, 2014 at 15:05
  • What version of .net and visual studio are you using? Your code works for me and I get the expected output of Hola!! Commented Sep 17, 2014 at 16:36

2 Answers 2

1

The RethrowIfAssertException is throwing an exception when

"Throws the exception again if it is an AssertFailedException or an AssertInconclusiveException"

so if that is throwing an exception it will never get to your "Hola" exception throw.

http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.testtools.unittesting.expectedexceptionbaseattribute.rethrowifassertexception.aspx

Sign up to request clarification or add additional context in comments.

5 Comments

I did exactly the same thing as the ExpectedException does. Also, removing the RethrowIfAssertException does not change anything. It still shows Exception has been thrown by the target of an invocation.
Have you put a break point in there to see if it is even getting to throw your "Hola" exception?
Yes I did. I updated my question and added a screenshot.
Have you looked through the exception that you get to see if the Hola exception was caught and rewrapped and thrown, or maybe look at the stack trace to see if something else is throwing an exception after this one?
I will use the Console.WriteLine to write information about the reason why the test fail.. Workaround again.. Thanks
0

it is working perfectly in VS 2013 Update 2: enter image description here

The Code:

    [TestClass]
    public class MyTests
    {
       [TestMethod]
       [ExpectedEngineExceptionAttribute]
       public void MyTest()
       {
           throw new Exception("HI");
       }
     }

    public class ExpectedEngineExceptionAttribute : ExpectedExceptionBaseAttribute
    {
       protected override void Verify(Exception exception)
       {
           RethrowIfAssertException(exception);
           throw new Exception("Hola!!!");
       }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.