我正在开发一个使用SQLite数据库的Xamarin应用程序,我们有单元测试来覆盖这个功能。
这些单元测试执行并传递OS X计算机但我们在Windows 8(64位)VM上运行它们时看到SQLite相关的错误(这在CI基础结构中充当了从属设备)。
我们看到的错误来自NUnit(2.6.4),当测试运行时
ProcessModel: Default DomainUsage: Single
Execution Runtime: net-4.5
Unhandled Exception:
System.BadImageFormatException: Could not load file or assembly 'App.Core.Tests, Version=1.0.5900.25009, Culture=neutral, PublicKeyToken=null' or one of its dependencies. An attempt was made to load a program with an incorrect format.
File name: 'App.Core.Tests, Version=1.0.5900.25009, Culture=neutral, PublicKeyToken=null'
Server stack trace:
at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
at System.Reflection.RuntimeAssembly.nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
at System.Reflection.Assembly.Load(AssemblyName assemblyRef)
at NUnit.Core.Builders.TestAssemblyBuilder.Load(String path)
at NUnit.Core.Builders.TestAssemblyBuilder.Build(String assemblyName, Boolean autoSuites)
at NUnit.Core.Builders.TestAssemblyBuilder.Build(String assemblyName, String testName, Boolean autoSuites)
at NUnit.Core.TestSuiteBuilder.BuildSingleAssembly(TestPackage package)
at NUnit.Core.TestSuiteBuilder.Build(TestPackage package)
at NUnit.Core.SimpleTestRunner.Load(TestPackage package)
at NUnit.Core.ProxyTestRunner.Load(TestPackage package)
at NUnit.Core.ProxyTestRunner.Load(TestPackage package)
at NUnit.Core.RemoteTestRunner.Load(TestPackage package)
at System.Runtime.Remoting.Messaging.StackBuilderSink._PrivateProcessMessage(IntPtr md, Object[] args, Object server, Object[]& outArgs)
at System.Runtime.Remoting.Messaging.StackBuilderSink.PrivateProcessMessage(RuntimeMethodHandle md, Object[] args, Object server, Object[]& outArgs)
at System.Runtime.Remoting.Messaging.StackBuilderSink.SyncProcessMessage(IMessage msg)
Exception rethrown at [0]:
at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
at NUnit.Core.TestRunner.Load(TestPackage package)
at NUnit.Util.TestDomain.Load(TestPackage package)
at NUnit.ConsoleRunner.ConsoleUi.Execute(ConsoleOptions options)
at NUnit.ConsoleRunner.Runner.Main(String[] args)
这与使用64位窗口的事实有关吗?
答案 0 :(得分:2)
BadImageFormat几乎总是因为正在运行的进程的位数与依赖程序集的位数之间存在不匹配。您可能知道,Sqlite不是AnyCPU,而是x86或x64。您的测试套件可能是编译为AnyCPU,但您使用的是32位版本的Sqlite。
NUnit 3将自动检测测试套件的位数(但不是依赖程序集)并正确运行它。 NUnit 2不会,您必须相应地运行测试。来自NUnit 2.6文档,
nunit-console程序的.NET 2.0版本是使用/ platform:anycpu构建的,这使得它在32位系统上进行jit编译为32位代码,在64位上进行64位代码系统。当NUnit用于在64位系统上测试32位应用程序时,这会导致异常。要避免此问题,请在64位系统上测试32位代码时,使用nunit-console-x86程序(使用/ platform:x86构建)。
写得不好,我们应该修理它,但我会推荐两件事;