运行 NUnit 在 TeamCity 上进行测试时抛出 BadImageFormatException

BadImageFormatException thrown when running NUnit tests on TeamCity

我有一个使用 NUnit 2.6.3 和 .Net 4.5 的单元测试项目。但是在 teamcity 构建步骤配置中,我只有 运行time 4.0 可用(即使我的解决方案的其他组件是针对 4.5 编译的)并且当我 运行 我的单元测试时,我得到以下异常:

    [Step 8/9] System.ArgumentException: Specified NUnit error running tests in '[SANITIZED].UnitTest.dll' assembly
Loading assembly is compiled for v4.0.30319, MSIL
NUnit runner runtime is v4.0.30319, x64
System.ArgumentException: A BadImageFormatException has been thrown while parsing the signature. This is likely due to lack of a generic context. Ensure genericTypeArguments and genericMethodArguments are provided and contain enough context. ---> System.BadImageFormatException: Could not load file or assembly '[SANITIZED]' or one of its dependencies. An attempt was made to load a program with an incorrect format.
   at System.ModuleHandle.ResolveType(RuntimeModule module, Int32 typeToken, IntPtr* typeInstArgs, Int32 typeInstCount, IntPtr* methodInstArgs, Int32 methodInstCount, ObjectHandleOnStack type)
   at System.ModuleHandle.ResolveTypeHandleInternal(RuntimeModule module, Int32 typeToken, RuntimeTypeHandle[] typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext)
   at System.ModuleHandle.ResolveTypeHandle(Int32 typeToken, RuntimeTypeHandle[] typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext)
   at System.Reflection.RuntimeModule.ResolveType(Int32 metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments)
   --- End of inner exception stack trace ---

Server stack trace: 
   at System.Reflection.RuntimeModule.ResolveType(Int32 metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments)
   at System.Reflection.CustomAttribute.FilterCustomAttributeRecord(CustomAttributeRecord caRecord, MetadataImport scope, Assembly& lastAptcaOkAssembly, RuntimeModule decoratedModule, MetadataToken decoratedToken, RuntimeType attributeFilterType, Boolean mustBeInheritable, Object[] attributes, IList derivedAttributes, RuntimeType& attributeType, IRuntimeMethodInfo& ctor, Boolean& ctorHasParameters, Boolean& isVarArg)
   at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeModule decoratedModule, Int32 decoratedMetadataToken, Int32 pcaCount, RuntimeType attributeFilterType, Boolean mustBeInheritable, IList derivedAttributes, Boolean isDecoratedTargetSecurityTransparent)
   at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeType type, RuntimeType caType, Boolean inherit)
   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)

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 JetBrains.TeamCity.NUnitLauncher.NUnitRunner.LoadTest(String testAssembly, String testName, TestRunner runner) in c:\BuildAgent\workf5b4bce579465c\src\NUnitLauncher\src\NUnit-2.6\NUnitRunner.cs:line 53
   at JetBrains.TeamCity.NUnitLauncher.AssemblyTest.LoadTestDomain() in c:\BuildAgent\workf5b4bce579465c\src\NUnitLauncher\src\AssemblyTest.cs:line 76
   at JetBrains.TeamCity.NUnitLauncher.LoadTestDomainAction.Action(AssemblyTest test) in c:\BuildAgent\workf5b4bce579465c\src\NUnitLauncher\src\LoadTestDomainAction.cs:line 17
   at JetBrains.TeamCity.NUnitLauncher.TryOneAssemblyTest.Action() in c:\BuildAgent\workf5b4bce579465c\src\NUnitLauncher\src\TryOneAssemblyTest.cs:line 18
   at JetBrains.TeamCity.NUnitLauncher.TryOneAssembly.Do() in c:\BuildAgent\workf5b4bce579465c\src\NUnitLauncher\src\TryOneAssembly.cs:line 36
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. description. description should be non-null string of (0, 4000] size
   at JetBrains.BuildServer.CommonLoggers.Impl.BuildProblemServiceMessageImpl.AddBuildProblem(String identity, String type, String description) in c:\BuildAgent\workf5b4bce579465c\src\CommonLoggers\src\Impl\BuildProblemServiceMessageImpl.cs:line 43
   at JetBrains.TeamCity.NUnitLauncher.TryOneAssembly.Do() in c:\BuildAgent\workf5b4bce579465c\src\NUnitLauncher\src\TryOneAssembly.cs:line 64
   at JetBrains.TeamCity.NUnitLauncher.NUnitRunnerBase.DoTry(TryOneAssembly aTry) in c:\BuildAgent\workf5b4bce579465c\src\NUnitLauncher\src\NUnitRunnerBase.cs:line 53
   at JetBrains.TeamCity.NUnitLauncher.NUnitRunnerBase.RunOneAssembly(AssemblyTest test, ITestOrderingStrategy strategy, Boolean isRecentTestMode) in c:\BuildAgent\workf5b4bce579465c\src\NUnitLauncher\src\NUnitRunnerBase.cs:line 161
   at JetBrains.TeamCity.NUnitLauncher.NUnitRunnerBase.RunInternal() in c:\BuildAgent\workf5b4bce579465c\src\NUnitLauncher\src\NUnitRunnerBase.cs:line 141
   at JetBrains.TeamCity.NUnitLauncher.NUnitRunnerBase.Run() in c:\BuildAgent\workf5b4bce579465c\src\NUnitLauncher\src\NUnitRunnerBase.cs:line 60

有什么想法可以帮助我配置这些单元测试吗?

根据堆栈跟踪和 NUnit release notes 我的印象是问题 不是 面向 v4.0 平台的 NUnit 2.6.3。

这更有可能是 NUnit 测试运行程序的位数问题,在您的工作中是 x64:

NUnit runner runtime is v4.0.30319, x64

如果您的 MSIL 编译测试程序集正在尝试加载某些 x86 库或类似的,则可以抛出 BadImageFormatException

只需将 TeamCity 作业的配置更改为使用 32 位 NUnit 运行程序(.NET 运行时 v4.0,x86)即可解决此问题。