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.
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.
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).
You can use Assert. ThrowsException<T> and Assert.
Gerrie pointed me in the right direction:
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 ;-)
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With