0

[Edited: 10-Aug]

I have a project that generates a DLL (for a private NuGet package). 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).

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.

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, almost tripled...each project contained a few tests that weren't applicable to the .NetFramework, so I used conditional compilation to remove these tests for the .NetFramework code.

All well and good....

However, when I push this up to Azure, then the test step fails.

The build script (yaml) contains the following commands:

...
- 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)

The "headline" news seems promising:

Job preparation parameters
6 queue time variables used
100% tests passed

And when I dig into this I see the following pattern per test project:

Total tests: 3650
Passed: 3650

Total tests: 3650
Passed: 3650

Total tests: 3652
Passed: 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.

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:

##[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. 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". But I'm not sure how to word that in yaml...


Edit 10th Aug #1

In response to Kevin Lu's comment below, I dug this out of the details.

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'. at Microsoft.VisualStudio.Coverage.DynamicCoverageDataCollectorImpl.Initialize(XmlElement configurationElement, IDataCollectionSink dataSink, IDataCollectionLogger logger) at Microsoft.VisualStudio.Coverage.DynamicCoverageDataCollector.OnInitialize(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'. 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 dataCollectorSettings, String settingsXml)..


Edit 10th Aug #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.

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?

DrGriff
  • 4,394
  • 9
  • 43
  • 92
  • 1
    Test with the same Dotnet test task settings and the `net472;net48;netcoreapp3.1` It could work in my side. The test will executed three times and the coverage data doesn't cause error. From the error message, I cannot get a valid error message.There are many possibilities to cause exit code 1. Can you share more error logs? You could set the `system.debug = true` and check the error log. – Kevin Lu-MSFT Aug 10 '20 at 09:06
  • @KevinLu-MSFT: I found in the details a section about not being able to load `CodeCoverageRunSettingsProcessor`. I've added this as an edit to the original post. – DrGriff Aug 10 '20 at 10:04
  • Glad to know that you could find the workaround. It seems that the vstest and dotnet test run in different ways. From the error message, it seems that this issue is related with package. I have shared my package information in the answer, you could refer to it. You could also share the package information with us. Then I will test the same package again. – Kevin Lu-MSFT Aug 11 '20 at 07:52
  • 1
    @KevinLu-MSFT: I edited the original post to add the packages in the TEST projects. The only one I'm missing is `Microsoft.TestPlatform.ObjectModel` but, as you'll see from my "10th Aug #2 edit", I'd tried adding that with no luck. I do have a slightly newer version of `Microsoft.NET.Test.Sdk` – DrGriff Aug 11 '20 at 08:05
  • Hi @DrGriff. Thanks for your information. The root cause could be the newer version of `Microsoft.NET.Test.Sdk`. Please check the update. You could try previous version. – Kevin Lu-MSFT Aug 11 '20 at 08:53
  • 1
    Hi @KevinLu-MSFT. Apologies for my tardiness, but was away. YES, I can confirm that `Microsoft.NET.Test.Sdk` v 16.6.1 works perfectly with `DotNetCoreCLI@2` for running tests, whilst 16.7.0 causes this issue (but works with `VSTest@2` as the test runner). – DrGriff Aug 18 '20 at 08:43

1 Answers1

2

From the error message, there seems to be some problems with the package you are using.

I would like to share the packages in my project.

Project.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. If I remove this package, the test will fail.

By the way, the build is running on Microsoft hosted agent: Windows-2019.

Update:

Test with the Microsoft.NET.Test.Sdk version: 16.7.0. I get the same issue.

enter image description here

You can try the previous version(e.g. 16.6.1).

Update2:

Microsoft.NET.Test.Sdk version 16.7.1 was released that addresses this.

nohwnd
  • 826
  • 7
  • 13
Kevin Lu-MSFT
  • 20,786
  • 3
  • 19
  • 28
  • 1
    I have the same issue as the op when I tried upgrading to 16.7.0. Using 16.6.1 works, however. – Andrew Moreno Aug 15 '20 at 16:50
  • @AndrewMoreno. Glad to know that the answer could give you some help. – Kevin Lu-MSFT Aug 17 '20 at 00:53
  • 1
    I can confirm that rolling back `Microsoft.NET.Test.Sdk` to v16.6.1 fixed this for me too when using 'DotNetCoreCLI@2' as the test runner. However, v16.7.0 does still work when using `VSTest@2` as the test runner. I will add a ticket with Microsoft. – DrGriff Aug 18 '20 at 08:45
  • @DrGriff. Glad to know that this workaround could give you some help. If possible, you can consider accepting this answer. It could help other community members who get the same issues , thanks. – Kevin Lu-MSFT Aug 18 '20 at 08:49
  • 1
    Ticket entered with [Microsoft](https://github.com/microsoft/vstest/issues/2523) (should you want to up-vote this) – DrGriff Aug 18 '20 at 08:58
  • 1
    We released 16.7.1 that addresses this. Sorry about the inconvenience. Please update your Test.SDK package (and ObjectModel as well, if you reference that directly as the project above does). – nohwnd Aug 20 '20 at 10:42