Andrew Maia
Andrew Maia

Reputation: 1

How I can fail VSTest@2 task in Azure DevOps pipeline if line coverage is below x %?

I want to force my Azure pipeline to fail if line coverage is below 60%.

It seems to be so easy. I already have the line coverage displayed in my pipeline results, but I couldn't find a way to force the VSTest@2 task to fail if line coverage is below 60%.:

Code coverage result displayed in my azure pipeline result

Can Somebody help me find a way to do this?

This my pipeline script:

# ASP.NET
# Build and test ASP.NET projects.
# Add steps that publish symbols, save build artifacts, deploy, and more:
# https://docs.microsoft.com/azure/devops/pipelines/apps/aspnet/build-aspnet-4

trigger: none

pool:
  vmImage: 'windows-latest'

variables:
  solution: '**/*.sln'
  buildPlatform: 'Any CPU'
  buildConfiguration: 'Release'

steps:
- task: NuGetToolInstaller@1

- task: NuGetCommand@2
  inputs:
    restoreSolution: '$(solution)'

- task: VSBuild@1
  inputs:
    solution: '$(solution)'
    msbuildArgs: '/p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:SkipInvalidConfigurations=true /p:PackageLocation="$(build.artifactStagingDirectory)"'
    platform: '$(buildPlatform)'
    configuration: '$(buildConfiguration)'

- task: VSTest@2
  inputs:
    platform: '$(buildPlatform)'
    configuration: '$(buildConfiguration)'
    codeCoverageEnabled: true
    runSettingsFile: '$(Build.SourcesDirectory)\src\Alper.SinergyIntegration.Tests.UnitTests\.runsettings'

.runsettings content file:

<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
  <DataCollectionRunSettings>
    <DataCollectors>
      <DataCollector friendlyName="Code Coverage">
        <Configuration>
          <CodeCoverage>
            <ModulePaths>            
              <Include>
                <ModulePath>alpe.sinerintegration.application.dll</ModulePath>
                <ModulePath>alpe.sinerintegration.core.dll</ModulePath>
              </Include>
            </ModulePaths>            
          </CodeCoverage>
        </Configuration>
      </DataCollector>
    </DataCollectors>
  </DataCollectionRunSettings>
</RunSettings>

My project is using net7.0 and xunit 2.4.2

Upvotes: 0

Views: 87

Answers (1)

Miao Tian-MSFT
Miao Tian-MSFT

Reputation: 5642

You can use this Build Quality Checks extension to make the builds with too low code coverage fail.

Add the following task after the vstest task.

- task: BuildQualityChecks@9
  inputs:
    checkCoverage: true
    coverageFailOption: 'fixed'
    coverageType: 'lines'
    coverageThreshold: '60'

When the code coverage is more than 60%, the pipeline is passed.

pass

When the code coverage is below 60%, the pipeline is failed.

fail

Upvotes: 1

Related Questions