Nunit: 引用不同版本的 NUnit 的 NUnit 属性导致测试无法运行

创建于 2020-01-01  ·  3评论  ·  资料来源: nunit/nunit

创建从TestActionAttribute派生的自定义属性,然后使用来自使用较新版本 NUnit 的测试库的该属性将导致 NUnit 不执行测试,因为它无法加载较旧的 NUnit 库。 使用app.config重定向到较新的 NUnit 版本可修复该问题,然后按预期运行测试。

这个问题并不明显,因为正常的测试似乎没有运行(状态:不确定)。 这是NUnit.ApplicationDomain 问题 #25:无法使用 VS2017 测试运行器运行的原因,我发现问题的根源只是因为参数化测试装置之一给出了以下堆栈跟踪:

An exception was thrown while loading the test.
System.IO.FileLoadException: Could not load file or assembly 'nunit.framework, Version=3.7.0.0, Culture=neutral, PublicKeyToken=2638cd05610744eb' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
File name: 'nunit.framework, Version=3.7.0.0, Culture=neutral, PublicKeyToken=2638cd05610744eb'
   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.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.Framework.Internal.TypeWrapper.GetCustomAttributes[T](Boolean inherit)
   at NUnit.Framework.Internal.Builders.DefaultSuiteBuilder.GetFixtureBuilderAttributes(ITypeInfo typeInfo)
   at NUnit.Framework.Internal.Builders.DefaultSuiteBuilder.BuildFrom(ITypeInfo typeInfo)

WRN: Assembly binding logging is turned OFF.
To enable assembly bind failure logging, set the registry value [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1.
Note: There is some performance penalty associated with assembly bind failure logging.
To turn this feature off, remove the registry value [HKLM\Software\Microsoft\Fusion!EnableLog].

  Exception doesn't have a stacktrace

潜在的问题 - 引用旧版本的 NUnit 的属性 - 可能会被关闭为“不会修复”,但我认为用户收到某种错误消息而不是让测试似乎拒绝执行可能会很有用.

我已经通过@_repo_NUnitAttributesTargettingDifferentVersion创建了一个复制解决方案

docs enhancement

最有用的评论

我同意这个结论:自定义属性被锁定到它们扩展的 NUnit 版本中这一事实并不新鲜,但也不容易被发现。检测冲突的框架版本并给出错误消息。

此外,在可能的情况下,我们应该记录如何编写向后兼容的自定义属性。 我相信仅依靠我们不变的接口并在配置文件中使用绑定重定向就可以解决问题,但有人应该先尝试一下。

所有3条评论

我同意这个结论:自定义属性被锁定到它们扩展的 NUnit 版本中这一事实并不新鲜,但也不容易被发现。检测冲突的框架版本并给出错误消息。

此外,在可能的情况下,我们应该记录如何编写向后兼容的自定义属性。 我相信仅依靠我们不变的接口并在配置文件中使用绑定重定向就可以解决问题,但有人应该先尝试一下。

我将其标记为两个文档,因此我们可以查看它们和增强功能,以调查检测冲突版本并可能推荐绑定重定向。

我不太熟悉 Roslyn 方面,但这是分析仪可以检测到的吗? 或者这是否必须是运行时检查(改进的错误消息)

此页面是否有帮助?
0 / 5 - 0 等级