Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 256
inva Avatar asked Nov 21 '11 14:11

inva


People also ask

What is NUnit used for?

NUnit provides a console runner (nunit3-console.exe), which is used for batch execution of tests. The console runner works through the NUnit Test Engine, which provides it with the ability to load, explore and execute tests.

What is the use of NUnit in C#?

The NUnit test runner contains the program entry point to run your tests. dotnet test starts the test runner using the unit test project you've created. In the unit-testing-using-nunit directory, run dotnet test again. The dotnet test command runs a build for the PrimeService project and then for the PrimeService.

How do I run a NUnit test in C#?

Navigate to Tools -> NuGet Package Manager -> Manager NuGet Packages for Solution. Search for NUnit & NUnit Test Adapter in the Browse tab. Click on Install and press OK to confirm the installation. With this installation, the NUnit framework can be used with C#.

What type of testing tool is NUnit?

NUnit is a unit testing tool ported initially from JUnit for . Net Framework and is completely an Open Source Project. NUnit was released in the early 2000s, and though the initial NUnit was ported from JUnit, the recent . Net version 3 is completely rewritten from scratch.


2 Answers

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.

like image 198
harlam357 Avatar answered Oct 17 '22 09:10

harlam357


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.

like image 41
Adam Kłobukowski Avatar answered Oct 17 '22 10:10

Adam Kłobukowski