由于 x86 - x64 - MSIL 差异,TeamCity NUnit 运行程序失败

TeamCity NUnit runner fails because of x86 - x64 - MSIL discrepancies

我有一堆项目的解决方案,其中一些也有测试项目。这些都是为 Any CPU 平台编译的。

在 TeamCity 中,我有一个 NUnit 2.6.4 的构建配置,我在上面指定了 Platform: auto (MSIL)Version: 4.0

当我 运行 构建时,测试失败并显示以下消息:

NUnit error running tests in 'C:\BuildAgent\work\a58fc6cd9bf63032\JsonApi.Tests\obj\Release\JsonApi.Tests.dll' assembly
Loading assembly is compiled for v4.0.30319, MSIL
NUnit runner runtime is v4.0.30319, x64

更奇怪的是,当我刚刚关闭 "Run recently failed tests first"(并更改 没有其他任何东西)时,我得到了以下内容:

NUnit error running tests in 'C:\BuildAgent\work\a58fc6cd9bf63032\JsonApi.Owin.Tests\obj\Release\JsonApi.Owin.Tests.dll' assembly
Loading assembly is compiled for v4.0.30319, MSIL
NUnit runner runtime is v4.0.30319, x86

请注意,在一种情况下,运行ner 是 x86,而在另一种情况下是 x64。在这两种情况下,他们都没有成功加载为 MSIL 编译的程序集。

我假设我的构建设置有问题,但是什么?

根据您提供的more detailed log,构建失败的真正原因似乎是:

...  
System.IO.FileNotFoundException: Could not load file or assembly 'System.Web.Http, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 or one of its dependencies.  
The system cannot find the file specified.  
File name: 'System.Web.Http, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35|'  

Server stack trace:  
    at System.Reflection.RuntimeAssembly.GetExportedTypes(RuntimeAssembly assembly, ObjectHandleOnStack retTypes)  
    at System.Reflection.RuntimeAssembly.GetExportedTypes()  
    at NUnit.Core.CoreExtensions.InstallAdhocExtensions(Assembly assembly)  
    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.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.SyncProcessMessage(IMessage msg)  
...  

而且我相信它的发生正是因为我在评论中链接的 SO thread 中提到的原因,即你是来自 obj[=26 的 运行 测试=] 文件夹,而它应该是 bin。但这仍然只是一个猜测,因为您没有提供您的 NUnit 步骤配置。

要解决此问题,您应该明确说明带有单元测试的程序集所在的路径。像这样:

**\bin\**\*.Tests.dll

在 "List assembly files:" 之下。这样来自 obj 文件夹的程序集将不会得到测试,请参阅 this SO thread.

在我的例子中,我得到了正确的文件夹,但 TeamCity 被设置为使用 nunit2.X 进行测试,而我已经针对 nunit3 进行了编译。在 TeamCity 更新测试步骤解决了这个问题。

我也遇到过类似的问题

Check correct .NET Framework version and process platform (x64, x86, MSIL) are used both in the test runner settings and involved assemblies on this machine.

在我的例子中,原因是 NUnit 框架在代码和 Teamcity runner 中有不同的版本。