Azure DevOps VSTest@2 does not generate code coverage

101 views Asked by At

I'm trying to generate code coverage and publish it using Azure DevOps pipelines.

Prerequisites:

  • My .NET version is 4.8
  • the coverlet.collector package is added to test projects
  • NUnit is used.

Here is the task I'm trying to produce coverage report with:

- task: VSTest@2
  displayName: "Run unit tests and analyze coverage"
  inputs:
    platform: "$(BuildPlatform)"
    configuration: "$(BuildConfiguration)"
    testAssemblyVer2: '**\*UnitTests.csproj'
    searchFolder: "$(System.DefaultWorkingDirectory)"
    resultsFolder: 'C:\temp\TestResults'
    runSettingsFile: 'Redacted\Solution\Redacted.runsettings'
    codeCoverageEnabled: false
    publishTestResults: false

My runsettings:

<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
    <DataCollectionRunSettings>
        <DataCollectors>
            <DataCollector friendlyName="XPlat code coverage">
                <Configuration>
                    <Format>Cobertura</Format>
                </Configuration>
            </DataCollector>
        </DataCollectors>
    </DataCollectionRunSettings>
</RunSettings>

Parts of logs which should be relevant:

...
2024-02-09T16:54:26.6295429Z DtaExecutionHost version 19.234.34524.1.
2024-02-09T16:54:26.9607038Z Starting TestExecution Model...
2024-02-09T16:54:31.0217094Z Result Attachments will be stored in LogStore
2024-02-09T16:54:31.1426722Z Run Attachments will be stored in LogStore
2024-02-09T16:54:31.1524406Z Result Attachments will be stored in LogStore
2024-02-09T16:54:31.2868265Z Result Attachments will be stored in LogStore
2024-02-09T16:54:31.2868899Z Run Attachments will be stored in LogStore
2024-02-09T16:54:31.3030070Z Provided settings file:
2024-02-09T16:54:31.3046512Z <?xml version="1.0" encoding="utf-8"?>
2024-02-09T16:54:31.3046955Z <RunSettings>
2024-02-09T16:54:31.3047230Z     <DataCollectionRunSettings>
2024-02-09T16:54:31.3047530Z         <DataCollectors>
2024-02-09T16:54:31.3047860Z             <DataCollector friendlyName="XPlat code coverage">
2024-02-09T16:54:31.3048281Z                 <Configuration>
2024-02-09T16:54:31.3048572Z                     <Format>Cobertura</Format>
2024-02-09T16:54:31.3048860Z                 </Configuration>
2024-02-09T16:54:31.3049138Z             </DataCollector>
2024-02-09T16:54:31.3049397Z         </DataCollectors>
2024-02-09T16:54:31.3049696Z     </DataCollectionRunSettings>
2024-02-09T16:54:31.3050000Z </RunSettings>
2024-02-09T16:54:31.3218580Z Updated Run Settings:
2024-02-09T16:54:31.3219071Z <RunSettings>
2024-02-09T16:54:31.3219348Z   <DataCollectionRunSettings>
2024-02-09T16:54:31.3219631Z     <DataCollectors>
2024-02-09T16:54:31.3219974Z       <DataCollector friendlyName="XPlat code coverage">
2024-02-09T16:54:31.3220301Z         <Configuration>
2024-02-09T16:54:31.3220594Z           <Format>Cobertura</Format>
2024-02-09T16:54:31.3220870Z         </Configuration>
2024-02-09T16:54:31.3221124Z       </DataCollector>
2024-02-09T16:54:31.3221394Z     </DataCollectors>
2024-02-09T16:54:31.3221679Z   </DataCollectionRunSettings>
2024-02-09T16:54:31.3221979Z   <RunConfiguration>
2024-02-09T16:54:31.3222260Z     <BatchSize>1000</BatchSize>
2024-02-09T16:54:31.3222619Z     <ResultsDirectory>C:\temp\TestResults</ResultsDirectory>
2024-02-09T16:54:31.3222978Z   </RunConfiguration>
2024-02-09T16:54:31.3223229Z </RunSettings>
...

Also there's these:

...
2024-02-09T16:54:34.6478537Z Data collection : Unable to find a datacollector with friendly name 'XPlat code coverage'.
2024-02-09T16:54:34.6479232Z Data collection : Could not find data collector 'XPlat code coverage'
...

After sucessfull test run as a result I get only trx file and no xml report I need.

  • Initially I tried using Microsoft.CodeCoverage package with friendlyName="Code Coverage" in the runsettings, behaviour was exactly the same.
  • I repalced .dll with .csproj in testAssemblyVer2 arg, as per some related answers.
  • experimented with publishTestResults and codeCoverageEnabled args, to see if they affect anything.

At this point I'm out of ideas, and hope someone can at least point me in some direction.

1

There are 1 answers

0
wade zhou - MSFT On

For the VSTest@2 task, typically it should use test files not csproj as the testAssemblyVer2 value, also need to enable codeCoverageEnabled. You need to build the project firstly to generate the test files.

Please check the task guide for the details.

enter image description here

For your scenario, you can use dotnet test instead of VSTest@2 task. Yaml sample:

pool:
  vmImage: 'windows-latest'

steps:
- task: DotNetCoreCLI@2
  displayName: dotnet test
  inputs:
    command: 'test'
    arguments: '--configuration Release --collect:"XPlat Code Coverage"'

- task: PublishCodeCoverageResults@2
  inputs:
    summaryFileLocation: |
      $(Agent.TempDirectory)\**\coverage.*.xml

enter image description here

My .runsettings is same with yours. My test project file for your reference:

enter image description here