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

彼得·蒙克斯

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

系统详情

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

问题1

我有一个名为的测试程序集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个测试,这是正确的数字。

问题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\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代码这里解释

因此,我更新了我的问题,使其更加针对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可以同时用于NUnitMSTest。

Microsoft.VisualStudio.QualityTools.UnitTestFramework问题所解释,在我的开发计算机上安装了Visual Studio,意味着每次我运行测试都没有问题-可以在我的系统上找到相关的DLL。但是,除非明确安装,否则它在构建服务器上永远不可用。

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

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

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

通过TeamCity中的Console Runner使用NUnit 3时未找到ServiceStack许可证

使用TeamCity和Fakes框架进行构建和测试

NUnit 单元测试订单或 TeamCity 中的单独运行程序

单元测试时未找到自定义注释

使用TeamCity构建TeamCity

使用NUnit 3时,为什么在TeamCity 9上得到“不支持文件类型”?

使用 NUnit-Console runner 按特定顺序运行 TestFixtures

测试时在 React 组件回调上使用 sinon fakes

如何对使用Console类的类进行单元测试?

nunit-console无法使用TestCase和TestCaseSource运行测试

使用nunit-console和mono运行硒测试

MsBuild无法与Teamcity中的NUnit一起使用

当使用任何CPU时,TeamCity .NET Runner错误地引用平台属性

使用Fakes进行单元测试会产生IConvertible异常

使用TeamCity构建本地项目以测试设置

使用Tox和Teamcity测试python项目

一个简单的单元测试,运行它时,得到错误“未找到测试”

API repo上有PR时如何使用TeamCity运行Android应用程序?

单独运行测试用例 Nunit Console Runner

使用具有TestCase属性的nunit3-console.exe进行参数化测试

有对TeamCity的测试

为dotTrace添加一个TeamCity构建步骤,并为所有单元测试设置阈值

使用.NET Core MSTest运行单元测试:“未找到以下TestContainer ...”

使用NUnit测试时写入日志文件

我想通过使用单元测试来帮助找到我的 java 程序中的所有缺陷

使用 Jest 和 Angular Cli 单元测试 Ag Grid 的列标题。无法从 columnDefs 中找到所有列

使用Teamcity使用Gradle构建Android项目时出错

使用NUnit使用异步/等待方法进行单元测试

在TestNG的单元测试中,使用mokito stub函数时总是报告nullPointerException,我没有找到解决方案