为了账号安全,请及时绑定邮箱和手机立即绑定

NUnit Console Runner 在使用 Kentico Fakes 时未找到所有单元测试

NUnit Console Runner 在使用 Kentico Fakes 时未找到所有单元测试

C#
跃然一笑 2022-07-23 16:45:59
我有 3 个 NUnit 测试程序集正在测试我为 Kentico CMS 平台编写的自定义代码。我可以在 Visual Studio(使用 NUnit 适配器)和 NUnit 控制台运行程序中运行这些测试。我现在正试图让这些测试在我的 TeamCity 构建服务器上运行。我已经设置了 NUnit 运行程序,它正在返回一些结果,但我发现并非所有测试都在执行。在一种情况下,我的一个程序集报告没有测试装置,尽管我知道肯定有。系统详情TeamCity v10.0.5(内部版本 42677)现在 3.8.1Nunit.ConsoleRunner 3.8.0肯蒂科 11Kentico.Libraries - 11.0.35Kentico.Libraries.Tests - 11.0.35问题 我有一个名为gto.ecommerce.core.tests.dll. 当它在 TeamCity 服务器上运行时,我得到这个输出(直接来自 NUnit 控制台运行器):NUnit Console Runner 3.8.0Copyright (c) 2018 Charlie Poole, Rob ProuseRuntime Environment   OS Version: Microsoft Windows NT 6.3.9600.0  CLR Version: 4.0.30319.42000Test Files    D:\TeamCity\buildAgent\work\4a231fb0e41e27f5\Tests\gto.ecommerce.core.tests\bin\Build\gto.ecommerce.core.tests.dllRun Settings    DisposeRunners: True    WorkDirectory: D:\TeamCity\buildAgent\work\4a231fb0e41e27f5\packages\NUnit.ConsoleRunner.3.8.0\tools    ImageRuntimeVersion: 4.0.30319    ImageTargetFrameworkName: .NETFramework,Version=v4.6    ImageRequiresX86: False    ImageRequiresDefaultAppDomainAssemblyResolver: False    NumberOfTestWorkers: 8Test Run Summary  Overall result: Passed  Test Count: 6, Passed: 6, Failed: 0, Warnings: 0, Inconclusive: 0, Skipped: 0  Start time: 2019-01-15 10:16:53Z    End time: 2019-01-15 10:16:54Z    Duration: 1.219 secondsResults (nunit3) saved as TestResult.xml这些与运行 TeamCity 构建步骤时返回的结果完全相同。但是,这里应该有更多的测试。如果我将 TeamCity 构建的二进制文件复制到本地计算机并运行相同的 NUnit 控制台运行程序命令,我会得到以下结果:NUnit Console Runner 3.8.0 Copyright (c) 2018 Charlie Poole, Rob ProuseRuntime Environment   OS Version: Microsoft Windows NT 10.0.14393.0  CLR Version: 4.0.30319.42000Test Files    C:\temp\gto-gtoengineering\TeamCity\gto.ecommerce.core.tests\Build\gto.ecommerce.core.tests.dllRun Settings    DisposeRunners: True    WorkDirectory: Z:\    ImageRuntimeVersion: 4.0.30319    ImageTargetFrameworkName: .NETFramework,Version=v4.6    ImageRequiresX86: False    ImageRequiresDefaultAppDomainAssemblyResolver: False    NumberOfTestWorkers: 8请注意我的机器如何说总共有 33 个测试,这是正确的数字。
查看完整描述

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。但是,除非明确安装,否则这在构建服务器上永远不可用。

解决方案

为了解决这个问题,我遵循了这个建议

  1. 复制Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll到我的解决方案中并将其提交给我的仓库。

  2. 在我的所有测试项目中包含对这个程序集的引用,确保Copy Local是真的。

通过简单地引用这个 DLL,构建会将它与所有其他依赖项一起复制到bin文件夹中,然后可以在它不是全局可用的环境中使用。


查看完整回答
反对 回复 2022-07-23
  • 1 回答
  • 0 关注
  • 157 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号