Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ExpectedException on TestMethod Visual Studio 2010

Tags:

Today I upgraded my solution with all the underlying projects from VS2008 to VS2010. Everything went well except for my unit tests.

First of all only the web projects had as target framework .NET 4. All the other projects still had .NET 3.5. I changed them all to .NET 4.

Now when I debug my unit tests it breaks on every exception. In 2008 it just wouldn't pass and tell me that an exception occurred. Even when I have the ExpectedException attribute defined it stops debugging on every exception.

And example of one of my tests:

[TestMethod]
[ExpectedException(typeof(EntityDoesNotExistException))]
public void ConstructorTest()
{
    AddressType type = new AddressType(int.MaxValue);
}

The EntityDoesNotExistException is a custom exception and inherits Exception.

Edit I looked at the Exceptions settings (ctrl+alt+e) in 2008 and 2010. In both versions the settings are the same. However in 2008 the debug doesn't break when I have the ExpectedException attribute. In 2010 it does break.

like image 739
Joop Avatar asked Apr 13 '10 11:04

Joop


People also ask

How do you test a method using exceptions?

Test for Exceptions using xUnitThrows<T> with an exception type, and an Action which is supposed to throw an exception. In our case, If num2 is zero then, capture the "ArithmeticException". We are going to test the below cases, num1 is non-zero and num2 is zero.

How do I run Unittest in Visual Studio?

To run all the tests in a default group, choose the Run icon and then choose the group on the menu. Select the individual tests that you want to run, open the right-click menu for a selected test and then choose Run Selected Tests (or press Ctrl + R, T).

Which assert method is used to validate that a method throws a particular exception or not?

You can use Assert. ThrowsException<T> and Assert.


2 Answers

Gerrie pointed me in the right direction:

  • Press Ctrl-Alt-E
  • Open the Common Language Runtime Excepions Node
  • Click Add
  • Type Microsoft.VisualStudio.TestTools.UnitTesting.AssertFailedException
  • Make sure that both checkboxes are unchecked.

This will get rid of the break on failed Asserts, but the test will still break when you have set an ExpectedException.

I was the one that set the 100 bonus for this, so some upvotes would be appreciated ;-)

like image 80
Dabblernl Avatar answered Sep 24 '22 20:09

Dabblernl


According to Microsoft, this is not a bug, it's "by design":

http://connect.microsoft.com/VisualStudio/feedback/details/511897/expectedexception-still-causes-debugging-to-break-with-exception-was-unhandled-by-user-code

like image 28
Victor Rodrigues Avatar answered Sep 25 '22 20:09

Victor Rodrigues