Joop
Joop

Reputation: 2839

ExpectedException on TestMethod Visual Studio 2010

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.

Upvotes: 20

Views: 10723

Answers (8)

Matthew Lock
Matthew Lock

Reputation: 13486

I ended up changing my tests to this form to avoid the breaking. Not ideal:

  [TestMethod]
  public void Guid()
  {
     try
     {
        Guid g = new Guid("myguid'123'");
     } 
     catch( FormatException fe)
     {
        return;  // expected exception - pass
     }

     Assert.Fail(); // exception not thrown - fail
  }

Upvotes: 0

Bill
Bill

Reputation: 41

I second what rlandster said. Clearing the "Enable Just My Code" debugging option fixed this for me. It fixed both breaking on assers and breaking on expected exceptions. There's a little more detail on this at http://social.msdn.microsoft.com/Forums/en/vsdebug/thread/25bdf149-5133-4f47-bbf2-1d4ca638fee9.

Upvotes: 1

rlandster
rlandster

Reputation: 7825

A Microsoft support guy told me to use Ctrl-F5 (start without debugging) when running my unit tests, and that seems to work.

Another thing to try is to go to Tools|Options|Debugging and un-check the "Enable Just My Code" option.

Upvotes: 2

Dabblernl
Dabblernl

Reputation: 16121

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 ;-)

Upvotes: 12

Einarsson
Einarsson

Reputation: 522

Make sure your reference to Microsoft.VisualStudio.QualityTools.UnitTestingFramework is Version 10.0.0.0.

If it is version 9.0.0.0 this problem will occur in Visual Studio 2010.

Hope this helps. If people still have this problem.

Upvotes: 1

Jon Mitchell
Jon Mitchell

Reputation: 3429

I've had the same issue, but finally managed to get it working. Not really sure how but here's a list of things I did between it not working to when it started working again.

  • Converted the project being tested to .NET 4
  • Turned off CodeCoverage
  • Turned CodeCoverage back on again
  • Did a RebuildAll on the test project

Not sure which bit fixed it though. Anyway, hope this helps!

Upvotes: 0

Gerrie Schenck
Gerrie Schenck

Reputation: 22368

Press Ctrl+Alt+E and check the break on exception setting for CLR exceptions. If this is set to true then the behavior you described will occur.

Upvotes: 2

Related Questions