Inclusing TRX files in SonarCloudAnalyze

  • ALM used Azure DevOps
  • CI system used Azure DevOps (YAML)
  • Scanner command used - task: SonarCloudAnalyze@1
  • Languages of the repository C#
  • Error observed Code coverage is 0% because it cannot find any TRX files. The log output says

2020-11-17T11:53:10.8683646Z Code coverage command line tool: C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Team Tools\Dynamic Code Coverage Tools\CodeCoverage.exe
2020-11-17T11:53:10.9803883Z Fetching code coverage report information from TFS…
2020-11-17T11:53:10.9832756Z Attempting to locate a test results (.trx) file…
2020-11-17T11:53:11.3080550Z Looking for TRX files in: D:\a\1\TestResults
2020-11-17T11:53:11.3085054Z No test results files found
2020-11-17T11:53:11.5686761Z Did not find any binary coverage files in the expected location.
2020-11-17T11:53:11.5710157Z Falling back on locating coverage files in the agent temp directory.
2020-11-17T11:53:11.5717338Z Searching for coverage files in D:\a_temp
2020-11-17T11:53:11.6570647Z No coverage files found in the agent temp directory.
2020-11-17T11:53:11.6572560Z Coverage report conversion completed successfully.

Latest version of SonarCloud extension installed in ADO version “1.20.0”
Note that SonarCloud integration is brand new for us and the Pipeline currently uses the following two steps to get covergae report

- script: reportgenerator -reports:$(Build.SourcesDirectory)\*\cobertura.xml -targetdir:$(Build.SourcesDirectory)\CodeCoverage -reporttypes:HtmlInline_AzurePipelines
  displayName: 'Create code coverage report'

- task: PublishCodeCoverageResults@1
  displayName: 'Publish code coverage report'
  inputs:
    codeCoverageTool: 'cobertura'
    summaryFileLocation: '$(Build.SourcesDirectory)/**/cobertura.xml'

Would the existing reportgenerator and PublishCodeCoverageResults steps be conflicting with SonarCloud?

Hi @pat_munkiisoft and welcome to the community !

The built-in mechanism for detecting Code Coverage (or Test files) inside the Scanner for .NET works as follow :

  • We try to detect if there’s any file inside the legacy TestResults folder inside the System.DefaultWorkingDirectory (this is what you see as “Looking for TRX files in: D:\a\1\TestResults”)
  • Then if no file found, we fallback into the TempDirectory, where most of the VsTest v2 task output is located.

Please note also that Cobertura is not supported for .NET code, you have to provide either Visual Studio Code Coverage, dotCover, OpenCover, Coverlet or NCover 3 for CodeCoverage, or VSTest, NUnit or xUnit for TestResults.

HTH,
Mickaël

I can see that our Unit Test tasks are outputting both a TRX file and a coverage report. An example output is

Results File: D:\a_temp\VssAdministrator_WIN-F042VB7G2IL_2020-11-18_20_52_34.trx

and

Calculating coverage result…
Generating report ‘D:\a\1\TestResults\pbf.opencover.xml’

Note I have switched to opencover rather than Cobertura after you said it was not supported. Does this look right?

I am still getting

Fetching code coverage report information from TFS…
Attempting to locate a test results (.trx) file…
Looking for TRX files in: D:\a\1\TestResults
No test results files found
Did not find any binary coverage files in the expected location.
Falling back on locating coverage files in the agent temp directory.
Searching for coverage files in D:\a_temp
No coverage files found in the agent temp directory.
Coverage report conversion completed successfully.

Just to make sure : Is your test task before the SonarCloud Run Code analysis task ?

I deleted my last reply because it was nonsense. Yes our test tasks run before the code analysis task. They are also before the SonarCloudPrepare task.

@mickaelcaro do you have any other help you can give us?

Thanks

Hi @pat_munkiisoft

Where does your trx come from ? Is that from a VSTest task on Azure ?

Have you tried setting up your opencover file path in the sonar.cs.opencover.reportsPaths property ?

Mickaël

I’ve tried setting the but that didn’t help.

Can you explain the relationship between the trx files that the SonarCloudAnalyze is saying it cannot find and the CoverletOutput XML files that I am point Sonarsource at with the sonar.cs.opencover.reportsPaths property?

We did have three seperate DotNetCoreCLI - ‘test’ tasks but I have cut that down for simplicity sake. These are the SonarCloudPrepare, DotNetCoreCLI and SonarCloudAnalyze steps

    - task: SonarCloudPrepare@1
      inputs:
        SonarCloud: 'Sonarcloud Connection'
        organization: 'myorg'
        scannerMode: 'MSBuild'
        projectKey: 'myorg_PMA'
        projectName: 'PMA'
        extraProperties: |
            sonar.cs.opencover.reportsPaths=$(Common.TestResultsDirectory)/pbf.opencover.xml
            sonar.verbose=true

    - task: DotNetCoreCLI@2
      displayName: 'Run unit tests - Forms'
      inputs:
        command: 'test'
        arguments: '--no-build --configuration $(buildConfiguration) /p:CollectCoverage=true /p:CoverletOutputFormat=opencover /p:CoverletOutput=$(Common.TestResultsDirectory)\pbf.opencover.xml'
        publishTestResults: true
        projects: '**/PBA.Forms.UnitTests.csproj'

- task: SonarCloudAnalyze@1

I am now getting code coverage appearing in SonarSource report.

Thanks for you help, I beleive the key change was settting sonar.cs.opencover.reportsPaths

2 Likes

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