NUnit C # Test Project referencing another DLL

I looked at a couple of problems with nunit and visual studio described in stackoverflow, but cannot find the thread where my case fits.

I use NUnit to check the code that I wrote, I upload the * .csproj file of my test project to the NUnit GUI tool.

I solved the problem, I think, but still have not received a solution. What am I doing:

I refer to 2 other projects as dll projects. This means that I got 3 projects: TestProject (DLL), SettingsManager (DLL), DatabaseInterface (DLL). They are all in one solution. The DatabaseInterface project contains its own api calls for another, C ++ x86, DLL, but does not reference the explicit link to this DLL through the using statement.

One of them is the Settings manager, which stores some configuration data, such as paths, etc., but anyway. Both Testproject and DatabaseInterface allude to SettingsManager.

All 3 projects are built in "Debug" and "AnyCPU". Summarizing and using only the ConfigurationManager in my TestProject works fine, but when I add the DatabaseInterface, I get a BadImageFormatException that says it is trying to load a file with the wrong format.

To make it more visible, this works:

using myNamespace.Settings; // contains SettingsManager using System; using NUnit.Framework; namespace myNamespace.myTestProject { [TestFixture] public class TestProject { [SetUp] public void SetUp() { } [Test] public void ReadDbFile() { string s = SettingsManager.DbFile; // gets the path of the db file } } } 

NUnit output:

This does not work:

 using myNamespace.Settings; // contains SettingsManager using myNamespace.DbInterface; // contains DatabaseInterface, which contains native calls to C++ dll using System; using NUnit.Framework; namespace myNamespace.myTestProject { [TestFixture] public class TestProject { DatabaseInterface instance = null; [SetUp] public void SetUp() { } [Test] public void ReadDbFile() { string s = SettingsManager.DbFile; // gets the path of the db file } } } 

Second attempt containing

 using myNamespace.DbInterface; 

throws myNamespace.myTestProject.TestProject (TestFixtureSetUp): SetUp: System.BadImageFormatException: Die Datei oder Assembly "DatabaseInterface, Version = 1.0.0.0, Culture = neutral, PublicKeyToken = null" or the link was not found. He tried to download the file in the wrong format.

even if all 3 projects are built using Debug and AnyCPU.

I use the standard * .config file, for example, one of the NUnit Testproject. Perhaps some are wrong about this.

Has anyone encountered the same error trying to load code from another dll? Could it be a problem that both projects (Test and Database) reference the SettingsManager DLL? Did I do something serious?

I double-checked the build configuration in all three projects, but could not find any settings that might be incorrect, and explains the BadImageFormatException exception.

+6
source share
2 answers

You are probably using the nunit.exe GUI runner, which is designed for any processor. This will be a JIT compiled on the target platform, which I assume is x64 as you have this problem. Instead, try using nunit-x86.exe to run the tests. This version of the GUI runner is specifically designed to run in a 32-bit process that will be compatible with your dependency of the DatabaseInterface library.

+5
source

This may be a dependency problem of your dependencies. This can happen if your dependency depends on the unmanaged COM library, which is x86, and you are running on x64. Everything will work fine until you try to use this dependency in your code - it will throw a BadImageFormatException .

To overcome this, you must add a specific object (x86 or x64) to your project and try with it.

+1
source

Source: https://habr.com/ru/post/902005/


All Articles