簡體   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