簡體   English   中英

Azure DevOps:解決多個.Net框架時測試失敗

[英]Azure DevOps: tests failing when addressing multiple .Net frameworks

[編輯:8 月 10 日]

我有一個生成 DLL 的項目(用於私有 NuGet 包)。 該項目被編寫為在以下框架中編譯:

<TargetFrameworks>netstandard2.0;netstandard2.1</TargetFrameworks>

該解決方案還包含四個測試項目,每個項目都是用 .NetCore 3.1 編寫的(使用上述項目中的 .Netstandard 2.1 dll)。

這些已經好幾個月了,但當然,我沒有測試我的 NuGet package 是否適用於使用 .NetStandard 2.0 框架的代碼。

因此,我更新了測試項目以在以下框架中編譯:

<TargetFrameworks>net472;net48;netcoreapp3.1</TargetFrameworks>

我立即將我的測試數量增加了兩倍,並且在 Visual Studio 中它們都通過了(呸),嗯。 幾乎翻了三倍……每個項目都包含一些不適用於 NetFramework 的測試。 所以我使用條件編譯來刪除 .NetFramework 代碼的這些測試。

一切都很好……

但是,當我將其推至 Azure 時,測試步驟將失敗。

構建腳本 (yaml) 包含以下命令:

...
- task: VSBuild@1
  displayName: 'Build all'
  inputs:
    solution: '$(solution)'
    platform: '$(buildPlatform)'
    configuration: '$(buildConfiguration)'
    maximumCpuCount: true
    
- task: DotNetCoreCLI@2
  displayName: 'Run Tests'
  inputs:
    command: 'test'
    projects: '**/*Test.csproj'
    arguments: '--configuration $(buildConfiguration) --collect "Code coverage"'
...

(僅供參考,我們還連接到 SonarCloud 並報告代碼覆蓋率)

“頭條”新聞似乎很有希望:

作業准備參數
使用了 6 個排隊時間變量
100% 測試通過

當我深入研究時,我看到每個測試項目的以下模式:

總測試:3650
通過:3650

總測試:3650
通過:3650

總測試:3652
通過:3652

所以,從上面我可以看出,這些數字對應於我的一個測試項目被執行了三次,一次是針對.Net 4.7.2,一次是針對.Net 4.8,一次是針對.Core 3.1。

對於其他每個測試項目,我看到了類似的三個執行組。

所以……每個測試都通過了。

但是,在每個測試項目的 .NetCore 版本執行之后(因此,已經執行了幾個月的版本),我現在得到以下信息:

##[error]Error: The process 'C:\Program Files\dotnet\dotnet.exe' failed with exit code 1
"C:\Program Files\dotnet\dotnet.exe" test d:\a\1\s\My.Test.Project\EMy.Test.Project.csproj --logger trx --results-directory d:\a\_temp --configuration Release --collect "Code coverage"

我不知道如何解釋它,因此如何解決它。

我的猜測是代碼分析報告正在接收 3 次覆蓋率數據,這導致了一些問題。 如果是這種情況,那么我不得不在 YAML 中說“像我們一直在做的那樣使用.NetCore 3.1 執行每個測試項目並收集代碼覆蓋率,然后再次執行它們,一次用於.Net 4.7。 2,然后是沒有代碼覆蓋的 4.8”。 但我不確定在 yaml 中如何表達...


編輯 8 月 10 日 #1

為了回應 Kevin Lu 在下面的評論,我從細節中挖掘了這一點。

數據收集器“代碼覆蓋率”消息:無法初始化代碼覆蓋率數據收集器並出現錯誤:System.TypeLoadException:無法從程序集“Microsoft.TestPlatform.Utilities,版本=15.0.0.0 加載類型“Microsoft.VisualStudio.TestPlatform.Utilities.CodeCoverageRunSettingsProcessor” ,文化=中性,PublicKeyToken=b03f5f7f11d50a3a'。 在 Microsoft.VisualStudio.Coverage.DynamicCoverageDataCollector.OnInitialize(XmlElement configurationElement,IDataCollectionSink dataSink,IDataCollectionLogger 記錄器)的 Microsoft.VisualStudio.Coverage.DynamicCoverageDataCollectorImpl.Initialize(XmlElement configurationElement)。 數據收集器“代碼覆蓋率”消息:數據收集器“代碼覆蓋率”在類型加載、構造或初始化期間引發異常:System.TypeLoadException:無法從程序集“Microsoft.VisualStudio.TestPlatform.Utilities.CodeCoverageRunSettingsProcessor”加載類型“Microsoft. TestPlatform.Utilities,版本=15.0.0.0,文化=中性,PublicKeyToken=b03f5f7f11d50a3a'。 在 Microsoft.VisualStudio.Coverage.DynamicCoverageDataCollectorImpl.Initialize(XmlElement configurationElement, IDataCollectionSink dataSink, IDataCollectionLogger logger) 在 Microsoft.VisualStudio.Coverage.DynamicCoverageDataCollector.OnInitialize(XmlElement configurationElement) 在 Microsoft.VisualStudio.TraceCollector.BaseDataCollector.Initialize(XmlElement configurationElement, IDataCollectionEvents 事件, IDataCollectionSink dataSink, IDataCollectionLogger logger, IDataCollectionAgentContext agentContext) 在 Microsoft.VisualStudio.TraceCollector.BaseDataCollector.Initialize(XmlElement configurationElement, DataCollectionEvents events, DataCollectionSink dataSink, DataCollectionLogger logger, DataCollectionEnvironmentContext environmentContext) 在 Microsoft.VisualStudio.TestPlatform.Common.DataCollector.DataCollectorInform () 在 Microsoft.VisualStudio.TestPlatform.Common.DataCollector.DataCollectionManager.LoadAndInitialize(DataCollectorSettings dataCol 講師設置,字符串 settingsXml)..


編輯 8 月 10 日 #2

我嘗試添加不同的 NuGet 包以查看是否可以使其正常工作,Microsoft.TestPlatform.ObjectModelMicrosoft.TestPlatform ),但無濟於事。

然后我將構建腳本從:

- task: DotNetCoreCLI@2
  displayName: 'Run Tests'
  inputs:
    command: 'test'
    projects: '**/*Test.csproj'
    arguments: '--configuration $(buildConfiguration) --collect "Code coverage"'

- task: VSTest@2
  displayName: 'Run Tests'
  inputs:
    testSelector: 'testAssemblies'
    testAssemblyVer2: |
      **\No1Test.dll
      **\No2Test.dll
      **\No3Test.dll
      **\No4Test.dll
    searchFolder: '$(System.DefaultWorkingDirectory)'
    codeCoverageEnabled: true
    runInParallel: true

這感覺像是倒退了一步,然而,所有的測試都通過了,沒有錯誤消息,所以這是值得高興的。 但是....這是“完美”的解決方案嗎?

從錯誤信息來看,您使用的 package 似乎存在一些問題。

我想分享我項目中的包。

項目.csproj

...
  <PropertyGroup>
    <TargetFrameworks>net472;net48;netcoreapp3.1</TargetFrameworks>
    <IsPackable>false</IsPackable>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" />
    <PackageReference Include="xunit" Version="2.4.1" />
    <PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" />
    <PackageReference Include="Microsoft.TestPlatform.ObjectModel" Version="16.6.1" />
  </ItemGroup>
...

Microsoft.NET.Test.Sdk package 與代碼覆蓋率有關。 如果我刪除這個 package,測試將失敗。

順便說一句,構建在Microsoft hosted agent: Windows-2019

更新:

使用Microsoft.NET.Test.Sdk版本進行測試:16.7.0。 我得到同樣的問題。

在此處輸入圖像描述

您可以嘗試以前的版本(例如 16.6.1)。

更新2:

Microsoft.NET.Test.Sdk版本 16.7.1 已發布以解決此問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM