VSTEST code coverage not being picked up

  • We have SQ Version 9.9 (build 65466)
  • We use VSTEST@2 ADO task to run binari tests (.dll)
  • We see the result in SQ to show the total unit tests run but it shows 0.0% Code coverage
  • I have tried running dotnet test and dotnet vstest and it doesn’t like it. Tests fails.
  • Tests and project is in .Net Framework 4.7.2
  • we use a self hosted ADO agent with windows and VS 2022 build tools
    Pipeline looks something like this.
- task: VisualStudioTestPlatformInstaller@1
  inputs:
    packageFeedSelector: 'nugetOrg'
    versionSelector: 'latestPreRelease'

steps:
- task: SonarQubePrepare@5
  inputs:
    SonarQube: 'DSS SonarQube Service Connection'
    scannerMode: 'MSBuild'
    projectKey: 'Caribou'
    projectName: 'Caribou'
    extraProperties: |
sonar.exclusions=xxx/Deploy/**,**/*.sql,**/*.xml,xxx/xxx.WebAPI.Tests.Integrated/**
      sonar.cs.vstest.reportsPaths=$(Agent.TempDirectory)\TestResults\*.trx

- task: NuGetCommand@2 <task details etc...>
- task: MSBuild@1 <task details etc...>

- task: VSTest@2
  inputs:
    testSelector: 'testAssemblies'
    testAssemblyVer2: |
      **\*test*.dll
      !**\*TestAdapter.dll
      !**\obj\**
    searchFolder: '$(System.DefaultWorkingDirectory)'
    resultsFolder: '$(Agent.TempDirectory)\TestResults'
    runTestsInIsolation: True
    runInParallel: True
    rerunFailedTests: True
    rerunFailedTestCasesMaxLimit: 3
    codeCoverageEnabled: true
    diagnosticsEnabled: true
  displayName: Run VSTS

- task: SonarQubeAnalyze@5
  displayName: 'Run SonarQube code analysis'
  timeoutInMinutes: 120
- task: SonarQubePublish@4
  displayName: 'Publish SonarQube analysis results'
  inputs:
    pollingTimeoutSec: '300'

In the logs I see this that stand out

.
.
.
##[debug]Code coverage was enabled. Attempting to add code coverage node to settings file.
##[debug]Adding code coverage settings details to runsettings file.
##[debug]Updating runsettings file from RunSettings node
##[debug]Successfully added code coverage settings details to runsettings file.
##[debug]Code coverage node present.

.
.
.
Results File: C:\agents09\_work\_temp\TestResults\saaacount_2023-03-31_15_51_52.trx
Test Run Successful.
Total tests: 9680
     Passed: 9680
 Total time: 4.4753 Minutes
.
.
.

Data collection : Unable to find a datacollector with friendly name 'Code Coverage'.
Data collection : Could not find data collector 'Code Coverage'
Data collection : Unable to find a datacollector with friendly name 'Code Coverage'.
Data collection : Unable to find a datacollector with friendly name 'Code Coverage'.
Data collection : Could not find data collector 'Code Coverage'
Data collection : Unable to find a datacollector with friendly name 'Code Coverage'.
Data collection : Unable to find a datacollector with friendly name 'Code Coverage'.
Data collection : Could not find data collector 'Code Coverage'
Data collection : Could not find data collector 'Code Coverage'
Data collection : Unable to find a datacollector with friendly name 'Code Coverage'.
Data collection : Could not find data collector 'Code Coverage'
Data collection : Could not find data collector 'Code Coverage'
Data collection : Logging DataCollector Diagnostics in file: C:\agents09\_work\_temp\Attempt-3_bus5lc.datacollector.23-03-31_15-51-41_85644_1.diag
Data collection : Unable to find a datacollector with friendly name 'Code Coverage'.
Data collection : Could not find data collector 'Code Coverage'
Data collection : Unable to find a datacollector with friendly name 'Code Coverage'.
Data collection : Could not find data collector 'Code Coverage'
.
.
.
.

This is how it looks on the GUI
image

Hi @Andres_D

SonarQube/SonarCloud does not run tests or generate reports, but imports pre-generated reports from another source. So, the first thing to do is check that the coverage reports are being generated correctly by the Microsoft tooling.

Which step in the build pipeline is producing the Data collection: Unable to find a datacollector... message?

My guess is that is the VSTest step, in which case the problem is probably to do with the set up of your build agent or build pipeline. You’re using the standard VSTest task, so the configuration in the pipeline should be correct. You’re using a self-hosted agent, so I suggest you confirm that the coverage tools are installed on the agent.

If the test coverage report is being produced, then have a look at our Troubleshooting .NET code coverage import guide.

yeah, the tools are installed not sure why I am getting that error. Will look into your suggestion, thank you and will report back with findings.

I was able to get this resolved. Our scenario is the following:

  • Application built in .Net Framework 4.7.2
  • ADO Self Hosted Agent

I did the following to get to be resolved:

  1. Download Microsoft Test Platform 17.5.0 from the PowerShell gallery on the VM.
  2. Change file extension from .nupkg to .zip. File name should be microsoft.testplatform.17.5.0.zip.
  3. Unzip the file contents in new folder C:\tools\
  4. Add path of code coverage to the path environmental variable. It should be “C:\tools\microsoft.testplatform.17.5.0\tools\net462\Team Tools\Dynamic Code Coverage Tools”

This way I can call codecoverage and convert the .coverage file to .coveragexml.

  • Added task to install Test platform version 17.5.0

  • Added these properties to SQ prepare:
    sonar.cs.vstest.reportsPaths=$(Agent.TempDirectory)\**\*.trx

    sonar.cs.vscoveragexml.reportsPaths=$(Agent.TempDirectory)\**\*.coveragexml

  • For VsTest task add the vsTestVersion: toolsInstaller

  • add powershell task to convert .coverage file to .coveragexml
    $coverageFileName = (get-childitem -Recurse -Include "*.coverage" | Select-Object -First 1 )
    codecoverage.exe analyze /output:output.coveragexml "$coverageFileName"

It should look something like this:

.
.
.
- task: VisualStudioTestPlatformInstaller@1
  inputs:
    packageFeedSelector: 'nugetOrg'
    versionSelector: '17.5.0'
  displayName: Installing Test Platform
- task: SonarQubePrepare@5
  inputs:
    SonarQube: 'SonarQube Service Connection'
    scannerMode: 'MSBuild'
    projectKey: 'Projectsample'
    projectName: 'Projectsample'
    extraProperties: |
      sonar.cs.vstest.reportsPaths=$(Agent.TempDirectory)\**\*.trx
      sonar.cs.vscoveragexml.reportsPaths=$(Agent.TempDirectory)\**\*.coveragexml
  displayName: SQ Prepare
.
.
.
project restore and build etc....
.
.
.
- task: VSTest@2
  inputs:
    testSelector: 'testAssemblies'
    testAssemblyVer2: |
      **\*test*.dll
      !**\*TestAdapter.dll
      !**\obj\**
    searchFolder: '$(System.DefaultWorkingDirectory)'
    resultsFolder: '$(Agent.TempDirectory)\TestResults'
    runTestsInIsolation: True
    runInParallel: True
    rerunFailedTests: True
    rerunFailedTestCasesMaxLimit: 3
    codeCoverageEnabled: true
    vsTestVersion: toolsInstaller
    testRunTitle: vstestResults
  displayName: Run VSTS

- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: |
     $coverageFileName = (get-childitem -Recurse -Include "*.coverage" | Select-Object -First 1)
     codecoverage.exe analyze /output:output.coveragexml "$coverageFileName"
    workingDirectory: '$(Agent.TempDirectory)\TestResults'
  displayName: Convert Coverage report to XML

#Run SQ scan
- task: SonarQubeAnalyze@5
  displayName: 'Run SonarQube code analysis'
  timeoutInMinutes: 120
- task: SonarQubePublish@4
  displayName: 'Publish SonarQube analysis results'
  inputs:
    pollingTimeoutSec: '300'
3 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.