locked
InnerException in Unit tests RRS feed

  • Question

  • Hello

    I use VS2008, and I have a unit test that is KO because my code throw an exception.

    The exception is displayed, but not the InnerException. How to display the InnerException message and StackTrace ?

    Thanks for advance. 

    Jpp

    Wednesday, February 23, 2011 4:04 PM

All replies

  • Hi,

    Can you do something like this in your unit test code:

     

     

    try

    {

     

     

    //your code

    }

     

     

    catch (Exception ex)

    {

     

     

    Console.WriteLine("Exception Message: " + ex.Message);

     

     

    Console.WriteLine("Exception InnerException: " + ex.InnerException.Message);

     

     

    Console.WriteLine("Exception StackTrace: " + ex.StackTrace);

    }

     

     

    Thanks,

    Anuj

    Thursday, February 24, 2011 12:37 AM
  • Hello

    You are rigth, it's a solution.

    But I have about 100 unit tests, so I will need to modify all my 100 unit tests with this code...

    I don't want to modify all my 100 unit tests...

    Thanks for advance. 

    Jpp

    Thursday, February 24, 2011 4:20 PM
  • Hi Jpp,

    You can create a delegate and then a function which takes the delegate as input. You can handle the exceptions in this function which is used for all test cases. This way you wont have to do the exception handling in all test methods.

    Hope it helps.

    Thanks,

    Anuj

    Friday, February 25, 2011 1:11 AM
  • Wouldn't it be simpler if the test framework displayed the root cause of the exception, that is to say the inner exception? Who would report exceptions, for whatever reason, and not include the inner exception? Why does this glaring omission exist in Microsoft's test framework? Rather than forcing hundreds if not thousands of developers to hack their tests, Microsoft should fix the issue.  Sadly, Visual Studio 2010 has the same problem.  
    Tuesday, May 8, 2012 1:00 PM