简体   繁体   English

Azure DevOps:解决多个.Net框架时测试失败

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

[Edited: 10-Aug] [编辑:8 月 10 日]

I have a project that generates a DLL (for a private NuGet package).我有一个生成 DLL 的项目(用于私有 NuGet 包)。 This project is written to compile in the following frameworks:该项目被编写为在以下框架中编译:

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

The solution also contains four Test projects, each written in.NetCore 3.1 (consuming the.Netstandard 2.1 dll from the above project).该解决方案还包含四个测试项目,每个项目都是用 .NetCore 3.1 编写的(使用上述项目中的 .Netstandard 2.1 dll)。

These has been working fine for months, but of course, I'm not testing that my NuGet package will work for code consuming the.NetStandard 2.0 framework.这些已经好几个月了,但当然,我没有测试我的 NuGet package 是否适用于使用 .NetStandard 2.0 框架的代码。

I therefore updated the Test projects to compile in the following frameworks:因此,我更新了测试项目以在以下框架中编译:

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

I immediately tripled my number of tests, and in Visual Studio they all pass (phew.), Well.我立即将我的测试数量增加了两倍,并且在 Visual Studio 中它们都通过了(呸),嗯。 almost tripled...each project contained a few tests that weren't applicable to the,NetFramework.几乎翻了三倍……每个项目都包含一些不适用于 NetFramework 的测试。 so I used conditional compilation to remove these tests for the.NetFramework code.所以我使用条件编译来删除 .NetFramework 代码的这些测试。

All well and good....一切都很好……

However, when I push this up to Azure, then the test step fails.但是,当我将其推至 Azure 时,测试步骤将失败。

The build script (yaml) contains the following commands:构建脚本 (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"'
...

(fyi, we're also hooked into SonarCloud and report on code coverage) (仅供参考,我们还连接到 SonarCloud 并报告代码覆盖率)

The "headline" news seems promising: “头条”新闻似乎很有希望:

Job preparation parameters作业准备参数
6 queue time variables used使用了 6 个排队时间变量
100% tests passed 100% 测试通过

And when I dig into this I see the following pattern per test project:当我深入研究时,我看到每个测试项目的以下模式:

Total tests: 3650总测试:3650
Passed: 3650通过:3650

Total tests: 3650总测试:3650
Passed: 3650通过:3650

Total tests: 3652总测试:3652
Passed: 3652通过:3652

So, from the above, I can see from these numbers correspond to one of my test projects being executed three times, once for.Net 4.7.2, once for.Net 4.8 and once for.Core 3.1.所以,从上面我可以看出,这些数字对应于我的一个测试项目被执行了三次,一次是针对.Net 4.7.2,一次是针对.Net 4.8,一次是针对.Core 3.1。

And I see a similar group of three executions for each of the other Test projects.对于其他每个测试项目,我看到了类似的三个执行组。

So...every test passed.所以……每个测试都通过了。

However, after the.NetCore version of each Test project executes (so, the one that had been executing fine for months), I now get the following:但是,在每个测试项目的 .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"

I don't know how to interpret it, and thus how to fix it.我不知道如何解释它,因此如何解决它。

My guess is that the code analysis report is receiving the coverage data three times, which is causing it some problems.我的猜测是代码分析报告正在接收 3 次覆盖率数据,这导致了一些问题。 If that were the case then I'd have to say in the YAML "Execute each test project once using.NetCore 3.1 as we've been doing and collect the code coverage, but then execute them all again, once for.Net 4.7.2 and then 4.8 without the code coverage".如果是这种情况,那么我不得不在 YAML 中说“像我们一直在做的那样使用.NetCore 3.1 执行每个测试项目并收集代码覆盖率,然后再次执行它们,一次用于.Net 4.7。 2,然后是没有代码覆盖的 4.8”。 But I'm not sure how to word that in yaml...但我不确定在 yaml 中如何表达...


Edit 10th Aug #1编辑 8 月 10 日 #1

In response to Kevin Lu's comment below, I dug this out of the details.为了回应 Kevin Lu 在下面的评论,我从细节中挖掘了这一点。

Data collector 'Code Coverage' message: Failed to initialize code coverage datacollector with error: System.TypeLoadException: Could not load type 'Microsoft.VisualStudio.TestPlatform.Utilities.CodeCoverageRunSettingsProcessor' from assembly 'Microsoft.TestPlatform.Utilities, Version=15.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.数据收集器“代码覆盖率”消息:无法初始化代码覆盖率数据收集器并出现错误:System.TypeLoadException:无法从程序集“Microsoft.TestPlatform.Utilities,版本=15.0.0.0 加载类型“Microsoft.VisualStudio.TestPlatform.Utilities.CodeCoverageRunSettingsProcessor” ,文化=中性,PublicKeyToken=b03f5f7f11d50a3a'。 at Microsoft.VisualStudio.Coverage.DynamicCoverageDataCollectorImpl.Initialize(XmlElement configurationElement, IDataCollectionSink dataSink, IDataCollectionLogger logger) at Microsoft.VisualStudio.Coverage.DynamicCoverageDataCollector.OnInitialize(XmlElement configurationElement).在 Microsoft.VisualStudio.Coverage.DynamicCoverageDataCollector.OnInitialize(XmlElement configurationElement,IDataCollectionSink dataSink,IDataCollectionLogger 记录器)的 Microsoft.VisualStudio.Coverage.DynamicCoverageDataCollectorImpl.Initialize(XmlElement configurationElement)。 Data collector 'Code Coverage' message: Data collector 'Code Coverage' threw an exception during type loading, construction, or initialization: System.TypeLoadException: Could not load type 'Microsoft.VisualStudio.TestPlatform.Utilities.CodeCoverageRunSettingsProcessor' from assembly 'Microsoft.TestPlatform.Utilities, Version=15.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.数据收集器“代码覆盖率”消息:数据收集器“代码覆盖率”在类型加载、构造或初始化期间引发异常:System.TypeLoadException:无法从程序集“Microsoft.VisualStudio.TestPlatform.Utilities.CodeCoverageRunSettingsProcessor”加载类型“Microsoft. TestPlatform.Utilities,版本=15.0.0.0,文化=中性,PublicKeyToken=b03f5f7f11d50a3a'。 at Microsoft.VisualStudio.Coverage.DynamicCoverageDataCollectorImpl.Initialize(XmlElement configurationElement, IDataCollectionSink dataSink, IDataCollectionLogger logger) at Microsoft.VisualStudio.Coverage.DynamicCoverageDataCollector.OnInitialize(XmlElement configurationElement) at Microsoft.VisualStudio.TraceCollector.BaseDataCollector.Initialize(XmlElement configurationElement, IDataCollectionEvents events, IDataCollectionSink dataSink, IDataCollectionLogger logger, IDataCollectionAgentContext agentContext) at Microsoft.VisualStudio.TraceCollector.BaseDataCollector.Initialize(XmlElement configurationElement, DataCollectionEvents events, DataCollectionSink dataSink, DataCollectionLogger logger, DataCollectionEnvironmentContext environmentContext) at Microsoft.VisualStudio.TestPlatform.Common.DataCollector.DataCollectorInformation.InitializeDataCollector() at Microsoft.VisualStudio.TestPlatform.Common.DataCollector.DataCollectionManager.LoadAndInitialize(DataCollectorSettings dataCol在 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 lectorSettings, String settingsXml)..讲师设置,字符串 settingsXml)..


Edit 10th Aug #2编辑 8 月 10 日 #2

I tried adding different NuGet packages to see if I could get this to work i.e. Microsoft.TestPlatform.ObjectModel and then Microsoft.TestPlatform ), but to no avail.我尝试添加不同的 NuGet 包以查看是否可以使其正常工作,Microsoft.TestPlatform.ObjectModelMicrosoft.TestPlatform ),但无济于事。

I then changed the build script from:然后我将构建脚本从:

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

to

- 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

This felt like a retrograde step, however, all tests passed and no error messages so that's something to be pleased about.这感觉像是倒退了一步,然而,所有的测试都通过了,没有错误消息,所以这是值得高兴的。 But....is this the "perfect" solution?但是....这是“完美”的解决方案吗?

From the error message, there seems to be some problems with the package you are using.从错误信息来看,您使用的 package 似乎存在一些问题。

I would like to share the packages in my project.我想分享我项目中的包。

Project.csproj项目.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>
...

The Microsoft.NET.Test.Sdk package relates with the code coverage. Microsoft.NET.Test.Sdk package 与代码覆盖率有关。 If I remove this package, the test will fail.如果我删除这个 package,测试将失败。

By the way, the build is running on Microsoft hosted agent: Windows-2019 .顺便说一句,构建在Microsoft hosted agent: Windows-2019

Update:更新:

Test with the Microsoft.NET.Test.Sdk version: 16.7.0.使用Microsoft.NET.Test.Sdk版本进行测试:16.7.0。 I get the same issue.我得到同样的问题。

在此处输入图像描述

You can try the previous version(eg 16.6.1).您可以尝试以前的版本(例如 16.6.1)。

Update2:更新2:

Microsoft.NET.Test.Sdk version 16.7.1 was released that addresses this. Microsoft.NET.Test.Sdk版本 16.7.1 已发布以解决此问题。

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

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