繁体   English   中英

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 runner 并且它正在返回一些结果,但我发现并不是所有的测试都在执行。 在一种情况下,我的一个程序集报告没有测试装置,尽管我知道肯定有。

系统详情

  • TeamCity v10.0.5(内部版本 42677)
  • 纽特 3.8.1
  • Nunit.ConsoleRunner 3.8.0
  • 肯蒂科 11
    • Kentico.Libraries - 11.0.35
    • Kentico.Libraries.Tests - 11.0.35

问题一

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

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\work\4a231fb0e41e27f5\Tests\gto.ecommerce.core.tests\bin\Build\gto.ecommerce.core.tests.dll


Run 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: 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 控制台运行命令,我会得到以下结果:

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 个测试,这是正确的数字。

问题二

如果我在另一个程序集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\work\4a231fb0e41e27f5\Tests\rwy.common.core.tests\bin\Build\rwy.common.core.tests.dll


Errors, Failures and Warnings

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

Run 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: 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库的事实,特别是我使用 Kentico 的CMS.Tests库来帮助对我的自定义 Kentico 代码进行单元测试,如解释here .

因此,我更新了我的问题,使其更具体地针对 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 问题 - 因为被引用以帮助编写假 Info 对象和提供程序的 Kentico CMS.Tests.dll适用于 NUnitMSTest。

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

解决方案

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM