18

I have a C# unit test in the Visual Studio 2013 test framework which exercises CLI and native code. I would like to investigate the native portion of the code while executing the C# unit test. However running Test -> Debug -> All Tests runs the managed debugger, so break points in native code are not hit and I cannot trace from C# -> C++/CLI code like I can when running the program under a mixed mode debugger.

For example, this code in my unit test:

[TestMethod]
public void TestRoundTripEvaluate()
{
     var obj = new MyCLIObject();
     var roundtripped = RoundtripXml( obj );

     // I would like to trace into here to see why Equals returns false.
     // But the definition for MyCLIObject is in a CPP file and cannot be navigated 
     // to running the unit test because Visual Studio starts the debugger as "managed only"
     // when using Test -> Debug -> All Tests
     Assert.IsTrue( obj.Equals( roundtripped ) ); 
}

Looking at the project settings for the unit test project, everything under Debug is disabled, so I can't check Enable Native Code Debugging, which allows this behavior for a normal program.

How can I enable mixed-mode debugging or native only debugging when running a VS C# unit test?

Chris
  • 201
  • 2
  • 6

2 Answers2

17
  1. Go to the properties page for your unit test project (right-click on project in Solution Explorer, then click "Properties")
  2. Go to the "Debug" tab (4th from the top in the list on the left-hand side)
  3. Enabled the checkbox "Enable native code debugging"
  4. Debug your unit test - you can set breakpoints in either native or managed code, and you can step into either kind of code.

I just had the same problem as you and was able to make it work using these steps. Before enabling this checkbox, it did not work. For the record, I'm using VS2013 update 4.

enter image description here

Nik
  • 321
  • 2
  • 9
-1

I'm not aware of a way to debug the native code in VS. If you want to understand why two objects are not equal, you need to check what comparison is happening for that "equals" call.

try reading this: https://msdn.microsoft.com/en-us/library/ms173147(VS.80).aspx

since MyCLIObject is not the same type as RoundtripXml, unless the comparison in MyCLIObject is looking for that type of RoundtripXml, i would expect it to always return false.

you could call:

Assert.IsTrue( roundtripped.Equals(obj) ); 

then see what is happening in the RoundtripXml class's version of Equals() (assuming it isn't cpp/native).

00jt
  • 2,818
  • 3
  • 25
  • 29