How to force the deployment of P/Invoked C++ library together with the C# tests on VS Test Explorer?


I'm trying to make a unit test for a C# library, runnable through the standard Test Explorer UI in Visual Studio 2019. The exact framework, such as xUnit or NUnit, doesn't matter (although I would prefer to use one that is shipped with VS by default, to make it easier for other people to contribute to the project). The issue is that the library P/Invokes some C++ code from another project that it depends on.

This always results in errors such as

Message: System.DllNotFoundException : Unable to load DLL 'CppImplementation.dll' or one of its dependencies: The specified module could not be found. (Exception from HRESULT: 0x8007007E)

Looks like the issue is that running through [Test]->[Run]->[All Tests] completely ignores the Working Directory settings for any of the projects, and deploys the tests somewhere before running them. The C++ dll is not being deployed, so later the code fails to find it anywhere.

The way I'm calling the C++ methods from C# is:

[DllImport("CppImplementation.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
private static extern int someFunction();

I tried creating .testsettings file in the solution and setting it as active. There I checked "Enable deployment" and added the compiled C++ .dll to the list of "Additional files and directories to deploy" (which already looks weird, because it doesn't look like I can customize this list per configuration, and I would prefer to deploy the correct version, but for now I would be happy to make it work in at least one).

This seems to have no effect whatsoever. The .dll is not getting copied anywhere, and all tests still fail to find it.

I am looking for a way to either force the deployment, or maybe somehow reconfigure the tests to do the right thing anyway. The closer it is to "the normal way to do it in VS2019" the better, due to the nature of the project.

asked on Stack Overflow Jun 5, 2019 by ivte • edited Apr 20, 2020 by yacc

0 Answers

Nobody has answered this question yet.

User contributions licensed under CC BY-SA 3.0