在 TeamCity 上运行 NUnit 测试时抛出 BadImageFormatException [英] BadImageFormatException thrown when running NUnit tests on TeamCity

查看:61
本文介绍了在 TeamCity 上运行 NUnit 测试时抛出 BadImageFormatException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a unit test project using NUnit 2.6.3 using .Net 4.5. But in the teamcity build step configuration I only have runtime 4.0 available (even if other components of my solution are compile against 4.5) and when I run my unit test I get the following exception :

    [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\work\42f5b4bce579465c\src\NUnitLauncher\src\NUnit-2.6\NUnitRunner.cs:line 53
   at JetBrains.TeamCity.NUnitLauncher.AssemblyTest.LoadTestDomain() in c:\BuildAgent\work\42f5b4bce579465c\src\NUnitLauncher\src\AssemblyTest.cs:line 76
   at JetBrains.TeamCity.NUnitLauncher.LoadTestDomainAction.Action(AssemblyTest test) in c:\BuildAgent\work\42f5b4bce579465c\src\NUnitLauncher\src\LoadTestDomainAction.cs:line 17
   at JetBrains.TeamCity.NUnitLauncher.TryOneAssemblyTest.Action() in c:\BuildAgent\work\42f5b4bce579465c\src\NUnitLauncher\src\TryOneAssemblyTest.cs:line 18
   at JetBrains.TeamCity.NUnitLauncher.TryOneAssembly.Do() in c:\BuildAgent\work\42f5b4bce579465c\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\work\42f5b4bce579465c\src\CommonLoggers\src\Impl\BuildProblemServiceMessageImpl.cs:line 43
   at JetBrains.TeamCity.NUnitLauncher.TryOneAssembly.Do() in c:\BuildAgent\work\42f5b4bce579465c\src\NUnitLauncher\src\TryOneAssembly.cs:line 64
   at JetBrains.TeamCity.NUnitLauncher.NUnitRunnerBase.DoTry(TryOneAssembly aTry) in c:\BuildAgent\work\42f5b4bce579465c\src\NUnitLauncher\src\NUnitRunnerBase.cs:line 53
   at JetBrains.TeamCity.NUnitLauncher.NUnitRunnerBase.RunOneAssembly(AssemblyTest test, ITestOrderingStrategy strategy, Boolean isRecentTestMode) in c:\BuildAgent\work\42f5b4bce579465c\src\NUnitLauncher\src\NUnitRunnerBase.cs:line 161
   at JetBrains.TeamCity.NUnitLauncher.NUnitRunnerBase.RunInternal() in c:\BuildAgent\work\42f5b4bce579465c\src\NUnitLauncher\src\NUnitRunnerBase.cs:line 141
   at JetBrains.TeamCity.NUnitLauncher.NUnitRunnerBase.Run() in c:\BuildAgent\work\42f5b4bce579465c\src\NUnitLauncher\src\NUnitRunnerBase.cs:line 60

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

Any idea to help me to configure these unit tests ?

推荐答案

基于 stacktrace 和 NUnit 发行说明 我的印象是问题出在 不是针对 v4.0 平台的 NUnit 2.6.3.

Based on the stacktrace and NUnit release notes my impression is that the problem is not with NUnit 2.6.3 targeting v4.0 platform.

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

It more likely seems to be an issue with bitness of the NUnit test runner, which in your job is x64:

NUnit runner 运行时是 v4.0.30319,x64

NUnit runner runtime is v4.0.30319, x64

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

The BadImageFormatException can be then thrown if your MSIL compiled test assembly is trying to load some x86 library or similarly.

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

Simply changing the configuration of your TeamCity job to use 32bit NUnit runner (.NET runtime v4.0, x86) should fix the issue.

这篇关于在 TeamCity 上运行 NUnit 测试时抛出 BadImageFormatException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆