.net - 在 TeamCity 上运行 NUnit 测试时抛出 BadImageFormatException

标签 .net nunit teamcity

我有一个使用 NUnit 2.6.3 和 .Net 4.5 的单元测试项目。但是在 teamcity 构建步骤配置中,我只有运行时 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\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

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

最佳答案

基于堆栈跟踪和 NUnit release notes我的印象是 NUnit 2.6.3 目标 v4.0 平台的问题

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

NUnit runner runtime is v4.0.30319, x64

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

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

关于.net - 在 TeamCity 上运行 NUnit 测试时抛出 BadImageFormatException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30996905/

相关文章:

c# - 我如何将更多时间添加到 datetime.now 解析它然后重置回原始 datetime.now?

c# - C#/.NET 中的可变字符串编辑

c# - 测试资源管理器 (VS) 显示 '<Unknown project>'

c# - 为什么在 TeamCity 中运行测试比直接在 NUnit 中运行测试花费的时间更长?

.net - 使用 nUnit 和 FitNesse 的测试和持续集成架构方法

ssh - TeamCity SSH-Exec Build步骤,使用参数中的密码

c# - 未找到 WPF PresenterFramework.Aero2

c# - Razor 模板中的可空日期时间错误

c# - 在单元测试中何时使用 Assert.Catch 与 Assert.Throws

asp.net - 如何为模型设置 IPrincipal?