1 回答
TA贡献1775条经验 获得超11个赞
经过大量跟踪后,我发现原因不是 NUnit 或 TeamCity,而是因为我使用了Kentico的库,特别是我使用 Kentico 的CMS.Tests库来帮助对我的自定义 Kentico 代码进行单元测试,如此处所述。
因此,我更新了我的问题以更具体地针对 Kentico,并将在下面提供解决此问题的解决方案。
确定根本原因
经过一番互联网搜索后,我发现 NUnit 控制台运行器有一个--trace 命令行选项。通过传入,--trace=Verbose我能够将跟踪文件写入为控制台运行程序设置的工作目录,然后比较两台机器。我的本地开发机器显示所有装置都已正确找到,但 TeamCity 服务器会生成跟踪文件,其输出类似于:
InternalTrace: Initializing at level Debug
14:41:48.559 Debug [ 5] DefaultTestAssemblyBuilder: Loading D:\TeamCity\buildAgent\work\4a231fb0e41e27f5\Tests\rwy.common.core.tests\bin\Build\rwy.common.core.tests.dll in AppDomain domain-
14:41:48.570 Debug [ 5] DefaultTestAssemblyBuilder: Examining assembly for test fixtures
14:41:48.579 Debug [ 5] DefaultTestAssemblyBuilder: Found 12 classes to examine
14:41:48.691 Error [ 5] DefaultTestAssemblyBuilder: System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
File name: 'Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
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.IsCustomAttributeDefined(RuntimeModule decoratedModule, Int32 decoratedMetadataToken, RuntimeType attributeFilterType, Int32 attributeCtorToken, Boolean mustBeInheritable)
at System.Reflection.CustomAttribute.IsDefined(RuntimeMethodInfo method, RuntimeType caType, Boolean inherit)
at NUnit.Framework.Internal.Reflect.GetMethodsWithAttribute(Type fixtureType, Type attributeType, Boolean inherit)
at NUnit.Framework.Internal.TestFixture..ctor(ITypeInfo fixtureType, Object[] arguments)
at NUnit.Framework.Internal.Builders.DefaultSuiteBuilder.BuildFrom(ITypeInfo typeInfo)
at NUnit.Framework.Api.DefaultTestAssemblyBuilder.GetFixtures(Assembly assembly, IList names)
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].
我不明白的部分是参考,Microsoft.VisualStudio.QualityTools.UnitTestFramework因为这与 NUnit 无关 - 那是 MSTest 库,我的项目中根本没有 MSTest 单元测试。
那时我意识到这是一个 Kentico 问题——因为CMS.Tests.dll被引用来帮助编写虚假 Info 对象和提供程序的 Kentico适用于 NUnit和MSTest。
在我的开发机器Microsoft.VisualStudio.QualityTools.UnitTestFramework上安装了 Visual Studio,正如这个问题中所解释的那样,这意味着每次我运行测试时都没有问题 - 可以在我的系统上找到依赖的 DLL。但是,除非明确安装,否则这在构建服务器上永远不可用。
解决方案
为了解决这个问题,我遵循了这个建议:
复制
Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll到我的解决方案中并将其提交给我的仓库。在我的所有测试项目中包含对这个程序集的引用,确保
Copy Local是真的。
通过简单地引用这个 DLL,构建会将它与所有其他依赖项一起复制到bin文件夹中,然后可以在它不是全局可用的环境中使用。
- 1 回答
- 0 关注
- 157 浏览
添加回答
举报
