skip to Main Content
- script: |   
    git config --global http.https://dev.azure.com/<company>/.extraheader "AUTHORIZATION: bearer $(System.AccessToken)"
    
    # Now fetch the PR source branch using the token
    git fetch origin $(System.PullRequest.SourceBranch):$(System.PullRequest.SourceBranch)
    git checkout $(System.PullRequest.SourceBranch)
  displayName: 'Fetch and Checkout PR Source Branch'
  env:
    # This exposes the system access token to the script
    SYSTEM_ACCESSTOKEN: $(System.AccessToken)

- script: |
    git checkout $(System.PullRequest.SourceBranch)
  displayName: 'Checkout PR Source Branch'

- task: DotNetCoreCLI@2
  displayName: New Manifest for tool
  inputs:
    command: custom
    custom: 'new '
    arguments: tool-manifest



- task: PowerShell@2
  displayName: Set the prereleaseVersionNumber variable value
  inputs:
    targetType: 'inline'
    script: |
      [string] $dateTime = (Get-Date -Format 'yyyyMMddTHHmmss')
      [string] $prereleaseVersionNumber = "$(stableVersionNumber)-$(Build.SourceVersion)"
      Write-Host "Setting the prerelease version number variable to '$prereleaseVersionNumber'."
      Write-Host "##vso[task.setvariable variable=prereleaseVersionNumber]$prereleaseVersionNumber"

- task: PowerShell@2
  displayName: Set the versionNumber to the stable or prerelease version number based on if the 'main' branch is being built or not
  inputs:
    targetType: 'inline'
    script: |
      [bool] $isMainBranch = $$(isMainBranch)
      [string] $versionNumber = "$(prereleaseVersionNumber)"
      if ($isMainBranch)
      {
        $versionNumber = "$(stableVersionNumber)"
      }
      Write-Host "Setting the version number to use to '$versionNumber'."
      Write-Host "##vso[task.setvariable variable=versionNumber]$versionNumber"

- task: PowerShell@2
  displayName: Set the name of the build (i.e. the Build.BuildNumber)
  inputs:
    targetType: 'inline'
    script: |
      [string] $buildName = "$(versionNumber)_$(Build.SourceBranchName)"
      Write-Host "Setting the name of the build to '$buildName'."
      Write-Host "##vso[build.updatebuildnumber]$buildName"
- task: NuGetToolInstaller@1
  displayName: 'Install NuGet 5.8'
  inputs:
    versionSpec: '5.8.x'
    
- task: NuGetCommand@2
  displayName: 'Restore packages using NuGet'
  inputs:
    restoreSolution: '**/*.csproj'
     
- task: UseDotNet@2
  displayName: Use .NET 6.0
  inputs:
    packageType: 'sdk'
    version: '6.0.x'


- task: DotNetCoreCLI@2
  displayName: 'Add Coverlet.msbuild package'
  inputs:
    command: 'custom'
    custom: 'add'
    projects: '$(Build.SourcesDirectory)/Virtue/Virtue.API/Virtue.API.csproj'
    arguments: 'package coverlet.msbuild'
  

- task: DotNetCoreCLI@2
  displayName: 'dotnet restore Virtue.API'
  inputs:
    command: 'restore'
    projects: '$(Build.SourcesDirectory)/Virtue/Virtue.API/Virtue.API.csproj'
    includeNuGetOrg: true
    noCache: true

- task: SonarCloudPrepare@1
  inputs:
    SonarCloud: 'SonarCloud Service Connection'
    organization: '<company_name>'
    scannerMode: 'MSBuild'
    projectKey: 'dotnet'
    projectName: 'Dotnet'
    extraProperties: |
      sonar.projectBaseDir=$(Build.SourcesDirectory)
      sonar.exclusions=**/README.md,**/obj/**,**/*.dll
      sonar.inclusions=**/*.csproj
      sonar.cs.vscoveragexml.reportsPaths=$(Build.SourcesDirectory)/**/coverage.xml
      sonar.cs.opencover.reportsPaths=$(Build.SourcesDirectory)/**/coverage.opencover.xml
      sonar.cs.vstest.reportsPaths=$(Agent.TempDirectory)/*.trx
      sonar.pullrequest.key=$(System.PullRequest.PullRequestId)
      sonar.pullrequest.branch=$(System.PullRequest.SourceBranch)
      sonar.pullrequest.base=$(System.PullRequest.TargetBranch)


- task: DotNetCoreCLI@2
  inputs:
    command: 'build'
    projects: '**/*.csproj'
    arguments: '--configuration $(buildConfiguration)'


- task: DotNetCoreCLI@2
  displayName: 'Test ...'
  inputs:
    command: test
    projects: '**/*Tests/*.csproj'
    arguments: '--configuration $(BuildConfiguration) /p:CollectCoverage=true /p:CoverletOutputFormat=opencover --logger trx'
    testRunTitle: 'dotnet test'

- task: PublishCodeCoverageResults@1
  inputs:
    codeCoverageTool: 'Cobertura'
    summaryFileLocation: '$(Build.SourcesDirectory)/**/coverage.opencover.xml'
    reportDirectory: '$(Build.SourcesDirectory)/**/coverage/'

- script: |
    dotnet tool install -g dotnet-reportgenerator-globaltool
    reportgenerator -reports:$(Build.SourcesDirectory)/**/coverage.opencover.xml -targetdir:$(Build.SourcesDirectory)/CoverageReport -reporttypes:"HtmlInline;Cobertura"
  displayName: 'Generate Coverage Report'
  
- task: PublishTestResults@2
  displayName: "Publish Test Results"
  inputs:
    testResultsFormat: VSTest
    testResultsFiles: "$(Build.SourcesDirectory)/**.*trx"
    mergeTestResults: true
    condition: succeededOrFailed()

- task: SonarCloudAnalyze@1
  inputs:
    sonar.analysis.level: 'DOTNET'

- task: SonarCloudPublish@1
  inputs:
    pollingTimeoutSec: '300'

Why on pull request, it is not taking new lines from my feature branch. I could see zero coverage and no new lines. Please help me. It is on high priority.

I tried many ways but no luck. I would like to analyse new lines of code using pull request that triggers my azure pipeline

2

Answers


  1. Chosen as BEST ANSWER

    I could fix the issue. Thank you. Needed to modify pipeline script.


  2. There are some points for collecting code coverage that you need to know:

    1. By default, the output result files of test and code coverage are generated under the directory "$(Agent.TempDirectory)" rather than "$(Build.SourcesDirectory)".
      If you want to specify the output directory to a different one, On the dotnet test command, you can configure the arguments like as below.

      dotnet test --results-directory "$(Build.SourcesDirectory)/TestResults" -p:CollectCoverage=true -p:CoverletOutput="$(Build.SourcesDirectory)/TestResults/coverage/" . . .
      
    2. If your projects are .NET 5 or later, it is recommended using the NuGet package "coverlet.collector" instead of "coverlet.msbuild" in the test projects.

    3. In the pipeline, you can configure like as below.
      The dotnet test task will automatically publish test result by default (publishTestResults: true), if you want to use a separate PublishTestResults task to publish test result, you can set "publishTestResults: false". Then add a PublishTestResults task after the dotnet test task.

      steps:
      . . .
      
      - task: DotNetCoreCLI@2
        displayName: 'dotnet test'
        inputs:
          command: test
          projects: path/to/test/project.csproj
          arguments: '--no-build -c $(buildConfiguration) -p:CollectCoverage=true --collect:"XPlat Code Coverage" -- DataCollectionRunSettings.DataCollectors.DataCollector.Configuration.Format=opencover'
      
      - task: PublishCodeCoverageResults@2
        displayName: 'Publish code coverage results'
        inputs:
          summaryFileLocation: '$(Agent.TempDirectory)/**/coverage.opencover.xml'
          pathToSources: '$(Agent.TempDirectory)/**/coverage.opencover.xml'
      
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search