locked
Testing for Exceptions in Unit Tests RRS feed

  • Question

  • I can't figure out how to test for an exception in a unit test.  Is there an Assert. comand or something else to test for an expected exception?

    Thanks!
    Cindy
    Thursday, June 9, 2005 3:05 PM

Answers

  • If you want a test to fail if an Exception of a specific type is NOT thrown, then take a look at the [ExpectedException] Attribute. This takes a type, which specifies the exception that is expected. Eg:



    [TestMethod]
    [ExpectedException(typeof(ArgumentException))]
    public void TestMethod()
    { Class.MethodThatThrows(null); }

     


    Yours,
    Dominic
    Thursday, June 9, 2005 10:49 PM

All replies

  • If you want a test to fail if an Exception of a specific type is NOT thrown, then take a look at the [ExpectedException] Attribute. This takes a type, which specifies the exception that is expected. Eg:



    [TestMethod]
    [ExpectedException(typeof(ArgumentException))]
    public void TestMethod()
    { Class.MethodThatThrows(null); }

     


    Yours,
    Dominic
    Thursday, June 9, 2005 10:49 PM
  • Thank you Dominic.  That is exactly what I was looking for.
    Friday, June 10, 2005 1:09 PM
  • Hi,

    I have a similar question.  Folowing the steps above I get this error message:

    Test method TDD_DB_Dispositivos.DB_CadTipDsp_Test.Isr_Codigo_Nulo threw exception System.Exception, but exception System.ArgumentException was expected. Exception message:  System.Exception: Parameterized Query '(@CODTIPDSP int,@NOMTIPDSP varchar(8000),@DESTIPDSP varchar(57))' expects parameter @NOMTIPDSP, which was not supplied.

    I´m New on TDD and until now I couldn´t resolve this.
    Can anyone help me?

    Thanks,

    Daniel.
    Friday, July 22, 2005 7:34 PM
  • Change the Typeof() to typeof(Exception) rather than typeof(ArgumentException)
    Friday, July 22, 2005 11:17 PM
  • Thanks for the answer dhopton,

    I change the code and stills not working well, now I have this message:

    UTA017: TDD_DB_Dispositivos.DB_CadTipDsp_Test.Isr_Codigo_Nulo has invalid ExpectedException attribute. The type must inherit from Exception.

    Anyone know how I have to do ?

    Daniel
    Monday, July 25, 2005 12:18 PM
  • It looks like you have a custom exception?  Does your exception inherit from System.Exception or System.ApplicationException?  Something like this:

    /// <summary>
    /// My custom exception
    /// </summary>
    public class MyException : System.Exception
    {
    /// <summary>
    /// Default constructor.
    /// </summary>
    public MyException () : base()
    {
    }

    /// <summary>
    /// Constructor accepting a string message.
    /// </summary>
    /// <param name="message">Exception message.</param>
    public MyException (string message) : base(message)
    {
    }

    ///
    <summary>
    /// Constructor accepting a string message and an
    /// inner exception which will be wrapped by this
    /// custom exception class.
    /// </summary>
    /// <param name="message">Exception message.</param>
    /// <param name="inner">Inner exception instance.</param>
    public MyException(string message, Exception inner) : base(message, inner)
    {
    }

    }

    Monday, July 25, 2005 12:28 PM
  • Hi Cindy,

    I just check with our developers and the answer is no, we don´t use custom exceptions, only the system exceptions at all...
    Monday, July 25, 2005 1:28 PM
  • From a design perspective, you should throw ApplicationException, no Exceptions -- it's just good practice :)

    However, there is definately a "bug" perse, here, because right now you can't use it for Exception, because Exception does not derive from it'sself. :) It also means the unit test framework can't catch non-CLR exceptions.

    I have logged this as an issue so that we might be able to fix it for RTM.

    Monday, July 25, 2005 9:24 PM
  • The other option is to Catch the Exception by hand in the test.
    Tuesday, July 26, 2005 5:23 PM
  • Hello,

    I just saw this as well.  Dhopton, you are correct in stating that you shouldn't throw a generic exception.  But I think a lot of folks think that if you give throw with a new message string in the exception, that will make the exception different enough.

    So, did this feature get into the final release?  It doesn't look like it.  I am useing the VS demo build, 50727.42 and I am seeing the same error message from the unit test, " has invalid ExpectedException attribute. The type must inherit from Exception."

    Do you know if they will add this in a patch?

     

    Thanks Dan!

     

    Friday, January 27, 2006 4:47 PM