NUnit C#Test Project引用其他DLL

inv*_*nva 6 .net c# nunit visual-studio

我浏览了stackoverflow上陈述的几个nunit和visual studio问题,但找不到我的情况适合的任何线程.

我正在使用NUnit来测试我编写的一些代码,我正在将我的testproject的*.csproj文件加载到NUnit GUI工具中.

我想出了我猜的问题,但到目前为止我没有解决方案.我在做什么:

我引用其他2个项目都是dll项目.这意味着我有3个项目:TestProject(DLL),SettingsManager(DLL),DatabaseInterface(DLL).所有都在一个解决方案.DatabaseInterface项目包含对另一个C++ x86 DLL的本机api调用,但不通过"using"语句显式引用此DLL.

其中一个是SettingsManager,存储一些配置数据,如路径等,但无论如何.Testproject和DatabaseInterface都引用了SettingsManager.

所有3个项目都在"Debug"和"AnyCPU"下构建.仅在我的TestProject中引用和使用SettingsManager工作正常,但是当我添加DatabaseInterface时,我得到一个BadImageFormatException,告诉我它正在尝试加载格式错误的文件.

为了使其更加明显,那就是:

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
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

NUnit输出:

这不起作用:

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
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

第二次尝试,包含

using myNamespace.DbInterface;
Run Code Online (Sandbox Code Playgroud)

抛出 myNamespace.myTestProject.TestProject(TestFixtureSetUp):SetUp:System.BadImageFormatException:Die Datei oder Assembly"DatabaseInterface,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null"或找不到引用.它试图加载错误格式的文件."

即使所有3个项目都是使用DebugAnyCPU构建的.

我正在使用标准的*.config文件,就像NUnit Testproject一样.也许有些人错了.

到目前为止,是否有人遇到同样的错误试图从另一个DLL加载代码?这两个项目(测试和数据库)引用SettingsManager DLL都可能是一个问题吗?我做了一件大错的事吗?

我在所有3个项目中仔细检查了我的构建配置,但是无法找到任何可能错误的设置并解释了BadImageFormatException.

har*_*357 5

您可能正在使用针对Any CPU的nunit.exe GUI运行器.它将被JIT编译到目标平台,我认为这是x64,因为你遇到了这个问题.相反,请尝试使用nunit-x86.exe来运行测试.此版本的GUI运行程序专门用于在32位进程中运行,该进程将与您的DatabaseInterface库依赖项兼容.