inva
inva

Reputation: 761

NUnit C# Test Project referencing other DLL

I browsed through couple of nunit and visual studio problems stated on stackoverflow, but can't find any thread where my case fits.

I'm using NUnit to test some code I wrote, I'm loading the *.csproj file of my testproject into NUnit GUI Tool.

I figured out the problem I guess but I got no solution so far. What I'm doing:

I reference 2 other projects both are dll projects. This means I got 3 projects: TestProject (DLL), SettingsManager(DLL), DatabaseInterface (DLL). All are in one solution. The DatabaseInterface project contains native api calls to another, C++ x86, DLL, but does not explicit reference this DLL via "using" statement.

One of both is a SettingsManager, storing some configuration data like paths & so on, but anyway. Both, Testproject as well as DatabaseInterface reference the SettingsManager.

All 3 projects are build in "Debug" and under "AnyCPU". Referencing & using only the SettingsManager in my TestProject works fine but when I add the DatabaseInterface, I got a BadImageFormatException telling me that it's trying to load a file having a wrong format.

To make it more visible, thats working:

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 doesn't 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
        }
    }
}

The second try, containing the

using myNamespace.DbInterface;

throws a myNamespace.myTestProject.TestProject(TestFixtureSetUp): SetUp : System.BadImageFormatException : Die Datei oder Assembly "DatabaseInterface, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" or a reference was not found. It was tried to load a file of a wrong format."

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

I'm using a standard *.config file, like the one of the NUnit Testproject. Maybe some is wrong with this.

Did anybody encountered the same error trying to load code from another DLL so far? Can it be a problem that both projects (Test & Database) reference the SettingsManager DLL ? Did I do something major wrong?

I double checked my build configuration in all 3 projects but was not able to find any settings which may be wrong and explains the BadImageFormatException.

Upvotes: 6

Views: 3361

Answers (2)

Adam Kłobukowski
Adam Kłobukowski

Reputation: 411

It might be a problem of dependency of your dependencies. It may happen if your dependency depends on COM unmanaged library that is x86 and you are running on x64. Everything will run fine, until you try to use that dependcy in your code - it will throw BadImageFormatException.

To overcome this you have to add specific target (x86 or x64) to your project, and try with that.

Upvotes: 1

harlam357
harlam357

Reputation: 1491

You are likely using the nunit.exe GUI runner which is targeted to Any CPU. It will be JIT compiled to the target platform, which I assume is x64 since you're having this issue. Instead, try using nunit-x86.exe to run your tests. This version of the GUI runner is targeted specifically to run in a 32-bit process which will be compatible with your DatabaseInterface library dependency.

Upvotes: 5

Related Questions