Debugging native (unmanaged) code in unit test

I have a .NET assembly written in C# which is using NUnit.

The tests in this assembly calls a C++/CLI-assembly.

The C++/CLI-assembly calls native/unmanaged C++ code.

When I try to debug a unit test using Resharper, Native debugging does not seem to be enabled in Visual Studio. This has the effect that I can not properly "step" in the native/unmanaged code in C++.


I currently use a cumbersome workaround:

1) Each test calls Debugger.Break(); the first thing it does.

2) I select to Run the unit test (not debug)

3) The Debugger.Break causes a dialog to be shown which allows me to select Visual Studio instance to perform the debugging.

4) I choose to manually select debugging type.

5) I select Native in the dialog which pops up.

6) I click OK


As mentioned, this is rather cumbersome. Anyone has a more straight-forward approach to debugging unit tests calling native code?

4 comments
Comment actions Permalink

we're currently having the same issue,

is there a reason for this limitation ? it's really driving us mad as most of our code is in an underlying c++ library.

has anyone found a solution ?

1
Comment actions Permalink

Ditto.  Would be nice to have an option to select the debugging engines in the Resharper settings.

0
Comment actions Permalink

There's a rather easy solution for this problem:

- Ensure your unit test assembly is the startup project (even if you won't be hitting F5 to start it)

- Enable native debugging in the project settings of that project

If still it doesn't start with native debugging enabled, set an external project (typically your exe) as startup program in debugging settings and start the exe once with native debugging enabled. After that, unit test should start with native debugging.

0
Comment actions Permalink

Here's a link towards a report of the same issue with Visual Studio's own test explorer.

From my non-insider point of view, and as far as I can tell, the internal issue must have been resolved on their side, as stepping in native code p/invoked from C# unit tests works for me.

0

Please sign in to leave a comment.