We have the following project setup:
- A native C++ project containing some custom native code
Example.Native
, producing a dll with the name example_native.dll - A wrapper project, defined in Swig, which wraps
Example.Native
, calledExample.Native.Wrapper
. It wraps the example_native.dll functions using typical P/Invoke calls and the[DllImport(...)]
attribute - A normal .NET (5.0) project
Example.Managed
, which referencesExample.Native.Wrapper
. There are no direct references to the native project itself - An executable project
Example.Runner
which referencesExample.Managed
and indirectly uses the native dlls without any issues. When running the project and inspecting the loaded modules I can see the example_native.dll is loaded
We want to write integration tests for the Example.Managed
project which includes the calls to Example.Native
, as the integration is relatively complex and isolated unit tests would not give us enough confidence. We use MSTest for testing
However, when we run the integration tests, we always get the exception
System.DllNotFoundException: Unable to load DLL 'example_native' or one of its dependencies: The specified module could not be found. (0x8007007E)
I already figured out the following:
- During test execution, the native dlls don't get loaded. They do not appear in the Modules list.
- The file exists in the working dir. Running
Assert.That(File.Exists("example_native.dll"))
is true. I also check the bin directory, and it exists there as well - I found a lot of similar questions, but most of them relate to older versions of visual studio, and their solutions don't seem to work
- .testsettings file which references the dlls. appears to not be supported anymore in VS2022, as it crashes when trying to edit the file. Doing so in VS2019 worked, but the tests still failed
- .runsetting file with the content below, did also not work, but honestly I'm not sure here if I included it correctly. I extended the project file with
<RunSettingsFilePath>...</RunSettingsFilePath>
<MSTest>
<DeploymentEnabled>False</DeploymentEnabled>
<AssemblyResolution>
<Directory path="path\to\dlls" includeSubDirectories="true"/>
</AssemblyResolution>
</MSTest>
- Possible related answers: Can't load DLL while executing tests with MS-Test
Is there any way I can get these dlls to load and my tests to run?