Home > OS >  SonarCloud Could not import unit test report TRX Unrecognized root element <TestRun>
SonarCloud Could not import unit test report TRX Unrecognized root element <TestRun>

Time:08-05

I'm new to testing and i'm trying to run my unit tests and coverage for .NET in Azure Pipelines and send it to SonarCloud. When i run my unit tests using VSTest I get the TRX file, and .coverage file. I'm using ReportGenerator to transform the .coverage file into XML and import the TRX and the coverage XML file to SonarCloud, but i'm receiving a error in the log when importing the TRX file.

This is the part of my pipeline I setup the tests:

  - task: SonarCloudPrepare@1
    inputs:
      SonarCloud: 'SonarCloud'
      organization: 'org'
      scannerMode: 'MSBuild'
      projectKey: 'org_git_dotnet'
      projectName: 'org_git / git / dotnet'
      projectVersion: '$(MajMinPat.Version)'
      extraProperties: |
        sonar.coverageReportPaths=$(Agent.BuildDirectory)\git\testsResult\unitTest\SonarQube.xml
        sonar.cs.nunit.reportsPaths=$(Agent.BuildDirectory)\git\testsResult\unitTest\testResults.trx

  - task: VisualStudioTestPlatformInstaller@1
    inputs:
      packageFeedSelector: 'nugetOrg'
      versionSelector: 'latestStable'

  - task: VSTest@2
    displayName: Unit Tests
    inputs:
      testSelector: 'testAssemblies'
      testAssemblyVer2: '*(*.Test)*(*.Integration).dll'
      searchFolder: '$(Agent.BuildDirectory)\git\test-bin'
      resultsFolder: '$(Agent.BuildDirectory)\git\testsResult\unitTest'
      vsTestVersion: 'toolsInstaller'
      testFiltercriteria: 'TestCategory!=Integration'
      codeCoverageEnabled: true
      otherConsoleOptions: '/Logger:"trx;LogFileName=testResults.trx" /EnableCodeCoverage -- DataCollectionRunSettings.DataCollectors.DataCollector.Configuration.CoverageFileName="my.coverage"'

  - task: PowerShell@2
    displayName: Search for coverage file
    inputs:
      targetType: 'inline'
      script: 'Get-ChildItem -Path "$(Agent.BuildDirectory)\git\testsResult\unitTest\*" -Include *.coverage -Recurse | Copy-Item -Destination $(Agent.BuildDirectory)\git\testsResult\unitTest'

  - task: PowerShell@2
    displayName: Transform coverage file into xml
    inputs:
      targetType: 'inline'
      script: 'C:\Users\tfs_build_agent\.nuget\packages\microsoft.codecoverage\17.2.0\build\netstandard1.0\CodeCoverage\CodeCoverage.exe analyze /output:$(Agent.BuildDirectory)\git\testsResult\unitTest\DynamicCodeCoverage.coveragexml $(Agent.BuildDirectory)\git\testsResult\unitTest\my.coverage'
      
  - task: DotNetCoreCLI@2
    displayName: Install Report Generator
    inputs:
      command: 'custom'
      custom: 'tool'
      arguments: 'install dotnet-reportgenerator-globaltool --version 4.8.13'

  - task: DotNetCoreCLI@2
    displayName: Run Report Generator
    inputs:
      command: 'custom'
      custom: 'reportgenerator'
      arguments: '-reports:$(Agent.BuildDirectory)\git\testsResult\unitTest\DynamicCodeCoverage.coveragexml -targetdir:$(Agent.BuildDirectory)\git\testsResult\unitTest -reporttypes:SonarQube'

   - task: SonarCloudAnalyze@1

The error I'm receiving:

Could not import unit test report 'C:\agent\_work\162\git\testsResult\unitTest\testResults.trx': Unrecognized root element <TestRun> in C:\agent\_work\162\git\testsResult\unitTest\testResults.trx at line 2

I'm not sure what i'm doing wrong here!

CodePudding user response:

Turns out i had to change at extraProperties the sonar.cs.nunit.reportsPaths to sonar.cs.vstest.reportsPaths

  • Related