Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using mixed-mode debugging on a managed unit test in Visual Studio 2013

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?

like image 429
Chris Avatar asked Dec 25 '14 20:12

Chris


People also ask

How do I Debug managed code in Visual Studio?

For Visual Basic, select Debug in the left pane, select the Enable native code debugging check box, and then close the properties page to save the changes. Select Debug in the left pane, select the Enable native code debugging check box, and then close the properties page to save the changes.

How do I Debug unit tests in Visual Studio?

To start debugging: In the Visual Studio editor, set a breakpoint in one or more test methods that you want to debug. Because test methods can run in any order, set breakpoints in all the test methods that you want to debug. In Test Explorer, select the test method(s) and then choose Debug on the right-click menu.

How do I set debug mode in Visual Studio?

In Solution Explorer, right-click the project and choose Properties. In the side pane, choose Build (or Compile in Visual Basic). In the Configuration list at the top, choose Debug or Release. Select the Advanced button (or the Advanced Compile Options button in Visual Basic).


1 Answers

  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

like image 149
Nik Avatar answered Oct 05 '22 23:10

Nik