Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ExceptionAsserts & debugging your C# project in VS

We've been using NUnit & VisualStudio to write C# .NET code for a while now. Testing Exceptions was done in the style of

old syntax:

[Test]
[ExpectException(typeof(ExceptionType))] 
public void TestExceptionType()
{

}

Now NUnit has released version 2.5.2 which introduced Assert.Throws( Type expectedExceptionType, TestDelegate code ); This makes exception testing a whole lot more flexible. Our exception tests now look like this:

new syntax:

[Test]
public void TestWithNullBufferArgument()
{
   ArgumentNullException ex = Assert.Throws<ArgumentNullException>(() => _testInstance.TestFunction(null));

   // now you can examine the exception and it's properties
   Assert.AreEqual(ex.Message, "Argument was null");
}

Our problem is that if Assert.Throws is used Visual Studio will cough up a window showing an unhandled exception when NUnit (either console or GUI runner) is used to debug the program.

to clarify this: we've set the VS project containing the unit tests to run nunit-x86.exe when debugging. (See project properties, debugging tab, start action is set to run nunit-x86.exe)

This stops NUnit from continuing the tests. It is possible to continue debugging/unit testing by pressing F5 but this is not a viable solution.

Is there any way to avoid this? Putting a try...catch block around the Assert.Throws does nothing since the exception happens in the delegate code.

I hope someone can shed some light on this.

like image 711
Timo Kosig Avatar asked Jan 18 '10 15:01

Timo Kosig


1 Answers

The problem itself appears because most likely you have option Enable Just My Code turned on (Tools->Options->Debugging->General->Enable Just My Code).

"When this feature is enabled, the debugger displays and steps into user code ("My Code") only, ignoring system code and other code that is optimized or does not have debugging symbols" (see "General, Debugging, Options Dialog Box")

Normally you have a release version of nunit.framework.dll which does not have a corresponding nunit.framework.pdb file.

So there are 2 options:

  1. Disable "Just My Code" feature

  2. Download sources of nunit (from http://www.nunit.org/index.php?p=download), build them in debug mode, put all nunit.framework.* (dll, pdb, xml) into lib or other directory in your solution and reference that nunit.framework.dll in your test project.

Hope this helps.

like image 72
dh. Avatar answered Sep 23 '22 11:09

dh.