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

TeamCity - NUnit Console Runner not finding all unit tests when using Kentico Fakes

我有 3 个 NUnit 测试程序集正在测试我为 Kentico CMS 平台编写的自定义代码。我可以 运行 在 Visual Studio(使用 NUnit 适配器)和 NUnit 控制台 运行 中进行这些测试。我现在正尝试在我的 TeamCity 构建服务器上进行这些测试 运行。我已经安装了 NUnit 运行ner 并返回了一些结果,但我发现并非所有测试都在执行。在一种情况下,我的一个程序集报告没有测试夹具,尽管我知道肯定有。

系统详细信息

问题 1

我有一个名为 gto.ecommerce.core.tests.dll 的测试程序集。当这是 TeamCity 服务器上的 运行 时,我得到这个输出(直接来自 NUnit 控制台 运行ner):

NUnit Console Runner 3.8.0
Copyright (c) 2018 Charlie Poole, Rob Prouse

Runtime Environment
   OS Version: Microsoft Windows NT 6.3.9600.0
  CLR Version: 4.0.30319.42000

Test Files
    D:\TeamCity\buildAgent\worka231fb0e41e27f5\Tests\gto.ecommerce.core.tests\bin\Build\gto.ecommerce.core.tests.dll


Run Settings
    DisposeRunners: True
    WorkDirectory: D:\TeamCity\buildAgent\worka231fb0e41e27f5\packages\NUnit.ConsoleRunner.3.8.0\tools
    ImageRuntimeVersion: 4.0.30319
    ImageTargetFrameworkName: .NETFramework,Version=v4.6
    ImageRequiresX86: False
    ImageRequiresDefaultAppDomainAssemblyResolver: False
    NumberOfTestWorkers: 8

Test 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 seconds

Results (nunit3) saved as TestResult.xml

这些与 TeamCity 构建步骤为 运行 时返回的结果完全相同。

不过,这里应该有更多的测试。如果我将 TeamCity 构建的二进制文件复制到我的本地计算机和 运行 相同的 NUnit 控制台 运行ner 命令,我得到这些结果:

NUnit Console Runner 3.8.0 
Copyright (c) 2018 Charlie Poole, Rob Prouse

Runtime Environment
   OS Version: Microsoft Windows NT 10.0.14393.0
  CLR Version: 4.0.30319.42000

Test Files
    C:\temp\gto-gtoengineering\TeamCity\gto.ecommerce.core.tests\Build\gto.ecommerce.core.tests.dll


Run Settings
    DisposeRunners: True
    WorkDirectory: Z:\
    ImageRuntimeVersion: 4.0.30319
    ImageTargetFrameworkName: .NETFramework,Version=v4.6
    ImageRequiresX86: False
    ImageRequiresDefaultAppDomainAssemblyResolver: False
    NumberOfTestWorkers: 8

Test Run Summary
  Overall result: Passed
  Test Count: 33, Passed: 33, Failed: 0, Warnings: 0, Inconclusive: 0, Skipped: 0
  Start time: 2019-01-15 10:19:31Z
    End time: 2019-01-15 10:19:35Z
    Duration: 4.494 seconds

Results (nunit3) saved as TestResult.xml

请注意我的机器如何显示总共有 33 个测试,这是正确的数字。

问题 2

如果我 运行 在另一个程序集上使用相同的场景,rwy.common.core.tests,那么这是 TeamCity 服务器结果:

NUnit Console Runner 3.8.0
Copyright (c) 2018 Charlie Poole, Rob Prouse

Runtime Environment
   OS Version: Microsoft Windows NT 6.3.9600.0
  CLR Version: 4.0.30319.42000

Test Files
    D:\TeamCity\buildAgent\worka231fb0e41e27f5\Tests\rwy.common.core.tests\bin\Build\rwy.common.core.tests.dll


Errors, Failures and Warnings

1) Invalid : D:\TeamCity\buildAgent\worka231fb0e41e27f5\Tests\rwy.common.core.tests\bin\Build\rwy.common.core.tests.dl
l
Has no TestFixtures

Run Settings
    DisposeRunners: True
    WorkDirectory: D:\TeamCity\buildAgent\worka231fb0e41e27f5\packages\NUnit.ConsoleRunner.3.8.0\tools
    ImageRuntimeVersion: 4.0.30319
    ImageTargetFrameworkName: .NETFramework,Version=v4.6
    ImageRequiresX86: False
    ImageRequiresDefaultAppDomainAssemblyResolver: False
    NumberOfTestWorkers: 8

Test Run Summary
  Overall result: Failed
  Test Count: 0, Passed: 0, Failed: 0, Warnings: 0, Inconclusive: 0, Skipped: 0
  Start time: 2019-01-15 10:21:16Z
    End time: 2019-01-15 10:21:17Z
    Duration: 1.046 seconds

Results (nunit3) saved as TestResult.xml

但是,我再次将相同的二进制文件复制到我的本地计算机,我得到了这个结果:

NUnit Console Runner 3.8.0 
Copyright (c) 2018 Charlie Poole, Rob Prouse

Runtime Environment
   OS Version: Microsoft Windows NT 10.0.14393.0
  CLR Version: 4.0.30319.42000

Test Files
    C:\temp\gto-gtoengineering\TeamCity\rwy.common.core.tests\Build\rwy.common.core.tests.dll


Run Settings
    DisposeRunners: True
    WorkDirectory: Z:\
    ImageRuntimeVersion: 4.0.30319
    ImageTargetFrameworkName: .NETFramework,Version=v4.6
    ImageRequiresX86: False
    ImageRequiresDefaultAppDomainAssemblyResolver: False
    NumberOfTestWorkers: 8

Test Run Summary
  Overall result: Passed
  Test Count: 20, Passed: 20, Failed: 0, Warnings: 0, Inconclusive: 0, Skipped: 0
  Start time: 2019-01-15 10:22:55Z
    End time: 2019-01-15 10:23:00Z
    Duration: 4.242 seconds

Results (nunit3) saved as TestResult.xml

肯定应该有 20 个可用的测试,但是当 运行 在 TeamCity 服务器上时,它说找不到任何测试。

总结

所以有谁知道为什么两台不同的机器 运行 将我假设为相同版本的 NUnit 和控制台 运行 产生截然不同的结果?除了 Windows,我看不出版本号有任何明显差异,但我认为应该使用相同的 .NET 框架。

我确实使用了大量 TestCaseSourceTestFixtureSource 属性来增加我的参数化测试的测试计数,但我不确定这是原因 - 如果它在一台机器上工作我不明白为什么另一个会有所不同。

经过相当多的追踪后,我发现原因不是 NUnit 或 TeamCity,而是我使用的库来自 Kentico, specifically I was using Kentico's CMS.Tests library for helping unit test my custom Kentico code as explained here

因此,我更新了我的问题以更具体地针对 Kentico,并将在下面提供解决此问题的解决方案。

找出根本原因

经过一些互联网搜索后,我发现 NUnit 控制台 运行ner 有一个 --trace command line option。通过传入 --trace=Verbose,我能够将跟踪文件写入为控制台 运行ner 设置的工作目录,然后比较两台机器。我的本地开发机器显示已正确找到所有固定装置,但 TeamCity 服务器会生成具有类似于以下输出的跟踪文件:

InternalTrace: Initializing at level Debug
14:41:48.559 Debug [ 5] DefaultTestAssemblyBuilder: Loading D:\TeamCity\buildAgent\worka231fb0e41e27f5\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 问题 - 因为被引用以帮助编写 fake Info objects and providers 的 Kentico CMS.Tests.dll 适用于 NUnit MSTest .

在我的开发机器上 Microsoft.VisualStudio.QualityTools.UnitTestFramework 安装了 Visual Studio,如 this question 中所述,这意味着每次我 运行 测试都没有问题 - 依赖DLL 可以在我的系统上找到。但是,除非明确安装,否则这在构建服务器上永远不可用。

解决方案

为了解决这个问题,我遵循了 this advice:

  1. 已将 Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll 复制到我的解决方案中并将其提交到我的存储库。
  2. 在我的所有测试项目中包含对此程序集的引用,确保 Copy Local 为真。

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