Testing for Exceptions in Unit Tests

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

[164 byte] By [CindyBrucato] at [2008-2-4]
# 1
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

dhoptonMSFT at 2007-9-8 > top of Msdn Tech,Visual Studio Team System,Visual Studio Performance Tools (Profiler)...
# 2
Thank you Dominic. That is exactly what I was looking for.
CindyBrucato at 2007-9-8 > top of Msdn Tech,Visual Studio Team System,Visual Studio Performance Tools (Profiler)...
# 3
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.

DanielLima at 2007-9-8 > top of Msdn Tech,Visual Studio Team System,Visual Studio Performance Tools (Profiler)...
# 4
Change the Typeof() to typeof(Exception) rather than typeof(ArgumentException)
dhoptonMSFT at 2007-9-8 > top of Msdn Tech,Visual Studio Team System,Visual Studio Performance Tools (Profiler)...
# 5
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

DanielLima at 2007-9-8 > top of Msdn Tech,Visual Studio Team System,Visual Studio Performance Tools (Profiler)...
# 6
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)
{
}

}

CindyBrucato at 2007-9-8 > top of Msdn Tech,Visual Studio Team System,Visual Studio Performance Tools (Profiler)...
# 7
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...

DanielLima at 2007-9-8 > top of Msdn Tech,Visual Studio Team System,Visual Studio Performance Tools (Profiler)...
# 8

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.

dhoptonMSFT at 2007-9-8 > top of Msdn Tech,Visual Studio Team System,Visual Studio Performance Tools (Profiler)...
# 9
The other option is to Catch the Exception by hand in the test.
dhoptonMSFT at 2007-9-8 > top of Msdn Tech,Visual Studio Team System,Visual Studio Performance Tools (Profiler)...
# 10

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!

snotman at 2007-9-8 > top of Msdn Tech,Visual Studio Team System,Visual Studio Performance Tools (Profiler)...

Visual Studio Team System

Site Classified