azure-devopsazure-pipelinescode-coveragediff

Enable Code Coverage diff without a target/gate threshold in ADO


I have code coverage enabled and published in my ADO pull request pipeline. I'm looking at adding code coverage diff to it.

The Microsoft Learn Document doesn't mention how to fail a build if the target diff is not met.

I have 2 asks:

  1. How to fail the build if it doesn't meet the target
  2. How to avoid the build from failing. I want to wait for the team to get mature on this and slowly enable the gate check. Until then, I don't want it to fail the build.

Thanks in Advance.


Solution

  • I have code coverage enabled and published in my ADO pull request pipeline. I'm looking at adding code coverage diff to it.

    According to Configuring coverage settings, if you would like to change the default settings of the code coverage experience for pull requests, you must include a configuration YAML file named azurepipelines-coverage.yml at the root of your repo. Set the desired values in this file and it will be used automatically the next time the pipeline runs.

    For example,

    coverage:
      status:           # Code coverage status will be posted to pull requests based on targets defined below.
        comments: on    # Off by default. When on, details about coverage for each file changed will be posted as a pull request comment. 
        diff:           # Diff coverage is code coverage only for the lines changed in a pull request.
          target: 60%   # Set this to a desired percentage. Default is 70 percent
    

    enter image description here

    Result:

    enter image description here

    How to fail the build if it doesn't meet the target

    To fail the build if the code coverage doesn't meet the target, you can install extension Build Quality Checks. For example, fail the build if code coverage is lower than 60%.

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

    enter image description here

    There are many options you can select. See the details about this task from Build Quality Checks.