unit-testingazure-devopsazure-pipelinesazure-pipelines-yamlvstest

No code coverage attachments were found from the trx files on self hosted agent


I have setup a azure devops pipeline for the .net core 3.1.0 application , but not coverage files are being picked by sonarcloud. What could I be missing , this build is running a self hosted azure devops pipeline. Is there something else that is need to the agent where the build is running from?

 C:\agent\_work\_tasks\SonarCloudPrepare_14d9cde6-c1da-4d55-aa01-2965cd301255\2.2.0\classic-sonar-scanner-msbuild\SonarScanner.MSBuild.exe end
SonarScanner for MSBuild 6.2
Using the .NET Framework version of the Scanner for MSBuild
Post-processing started.
11:17:30.481  11:17:30.481  WARNING: File 'C:\agent\_work\1\s\Services\Internal\WebJob\WebJob_TemporaryKey.pfx' does not exist.
11:17:30.496  11:17:30.496  WARNING: File 'C:\agent\_work\1\s\Services\External\Api\Properties\PublishProfiles\vodacom-api-fs-sme-dev - Web Deploy.pubxml' does not exist.
11:17:30.496  11:17:30.496  WARNING: File 'C:\agent\_work\1\s\Tests\Functional\Helpers.Tests\libman.json' does not exist.
11:17:30.715  11:17:30.715  WARNING: File 'C:\agent\_work\1\s\Services\Internal\Credit.Internal.Api\Properties\PublishProfiles\api-credit-uat - FTP.pubxml' does not exist.
11:17:30.715  11:17:30.715  WARNING: File 'C:\agent\_work\1\s\Services\Internal\Credit.Internal.Api\Properties\PublishProfiles\api-credit-uat - ReadOnly - FTP.pubxml' does not exist.
Calling the TFS Processor executable...
Fetching code coverage report information from TFS...
Attempting to locate a test results (.trx) file...
Looking for TRX files in: C:\agent\_work\1\TestResults
The following test results files were found: C:\agent\_work\1\TestResults\agentdevopsvm1$_agentdevopsvm1_2024-07-22_11_15_24.trx
**No code coverage attachments were found from the trx files.**
Not using the fallback mechanism to detect binary coverage files.
Coverage report conversion completed successfully.

Below is the yaml file I have configired

    trigger:
    - feature/EE-14
    
    pool:
      name: DevOpsAgent
      
    
    variables:
    - name: BuildConfiguration
      value: Dev
    - name: BuildPlatform
      value: any cpu
    
    jobs:
    - job: Job_1
      displayName: Code Quality Job
       
    
      steps:
      - checkout: self
        clean: true
        fetchTags: false
      - task: VisualStudioTestPlatformInstaller@1
        inputs:
          packageFeedSelector: 'nugetOrg'
          versionSelector: 'latestStable'
      - task: NuGetToolInstaller@1
        displayName: NuGet install
        inputs:
          versionSpec: 6.6.1
    
      - task: NuGetCommand@2
        displayName: NuGet Restore
        inputs:
          command: restore
          includeSymbols: false
          includeNuGetOrg: true
          restoreSolution: '**/*.sln'
          
      
      - task: UseDotNet@2
        inputs:
          packageType: 'runtime'
          version: '3.1.0'
          installationPath: C:\Program Files\dotnet\
    
      - task: SonarCloudPrepare@2
        displayName: Prepare Sonar Cloud Analysis
        inputs:
          SonarCloud: '********'
          organization: '*******'
          scannerMode: 'MSBuild'
          projectKey: '*******'
          projectName: '*****'
         
    
      - task: VSBuild@1
        displayName: Build Solution
        retryCountOnTaskFailure: 1
        inputs:
          vsVersion: latest
          platform: $(BuildPlatform)
          configuration: $(BuildConfiguration)
          clean: true
      
      - task: VSTest@2
        displayName: Run Tests
        inputs:
          testAssemblyVer2: |
           Tests\Functional\*.*[Tt]ests\**\$(BuildConfiguration)\*[Tt]ests*.dll
           Tests\Functional\*.*[Tt]ests\**\$(BuildConfiguration)\net6.0\*[Tt]ests*.dll
           Tests\Functional\*.*[Tt]ests\**\$(BuildConfiguration)\netcoreapp3.1\*[Tt]ests*.dll
           !**\*dMVC.Tests.Common*
           !**\*Microsoft.VisualStudio.TestPlatform*
           !**\obj\**
          platform: '$(BuildPlatform)'
          configuration: '$(BuildConfiguration)'
          runInParallel: true 
          pathtoCustomTestAdapters: $(System.DefaultWorkingDirectory)\packages
          resultsFolder: 'C:\agent\_work\1\TestResults'
          codeCoverageEnabled: true
          # customCoverletOptions: '--format cobertura'  # Example for .NET Core with Coverlet
          diagnosticsEnabled: true
          
      - task: SonarCloudAnalyze@2
        displayName: Run Code Analysis
        inputs:
          jdkversion: 'JAVA_HOME_17_X64'
    
      - task: SonarCloudPublish@2
        inputs:
          pollingTimeoutSec: '300'

Project file:

    <Project Sdk="Microsoft.NET.Sdk">
    
      <PropertyGroup>
        <TargetFramework>netcoreapp3.1</TargetFramework>
    
        <IsPackable>false</IsPackable>
      </PropertyGroup>
    
      <ItemGroup>
        <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
        <PackageReference Include="NUnit" Version="3.14.0" />
        <PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
        <PackageReference Include="NUnit.Analyzers" Version="3.9.0">
          <PrivateAssets>all</PrivateAssets>
          <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
        </PackageReference>
        <PackageReference Include="coverlet.collector" Version="3.1.2" />
        <PackageReference Include="IdentityModel" Version="6.0.0" />
        <PackageReference Include="Refit.HttpClientFactory" Version="6.3.2" />
        <PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
      </ItemGroup>
    
      <ItemGroup>
        <ProjectReference Include="..\..\..\Services\Shared\Credit.Internal.Api.Client\Credit.Internal.Api.Client.csproj" />
      </ItemGroup>
    
      <ItemGroup>
        <None Update="Resources\blank.pdf">
          <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
        </None>
      </ItemGroup>
    
    </Project>

Solution

  • As per the error message, the test result trx was found but not for coverage report. You can confirm the VSTest@2 task result for both files, make sure they are generated correctly.

    enter image description here

    My Vsts task for your reference. Use $(Agent.BuildDirectory)\TestResults for the resultsFolder:

      - task: VSTest@2
        inputs:
          testSelector: 'testAssemblies'
          testAssemblyVer2: |
            **\$(BuildConfiguration)\*Test*.dll
            !**\*dMVC.Tests.Common*
            !**\*Microsoft.VisualStudio.TestPlatform*
            !**\obj\**
          searchFolder: '$(System.DefaultWorkingDirectory)'
          resultsFolder: '$(Agent.BuildDirectory)\TestResults'
          runInParallel: true
          codeCoverageEnabled: true
          platform: '$(BuildPlatform)'
          configuration: '$(BuildConfiguration)'
          diagnosticsEnabled: true
    

    And SonarCloudAnalyze@2 can find the trx and coverage report successfully. enter image description here

    Edit:

    I can reproduce the same problem(Data collection : Unable to find a datacollector with friendly name 'Code Coverage'.) with self-hosted agent. It's fixed by changing to visual studio enterprise edition, as coverage is only available on this edition.

    enter image description here

    enter image description here