Lesson 4 of 4 Pipeline Testing

Implementing Code Coverage Analysis

This lesson explains what coverage measures, how to publish it, and how to use it as a gate.

Learning outcomes

After completing this lesson, you will be able to:

  • Distinguish line coverage from branch coverage.
  • Publish coverage in Azure Pipelines.
  • Configure diff coverage for pull requests.
  • Explain the limits of coverage as a target.

What coverage measures

A coverage tool instruments the code, runs the tests, and records which parts executed. Line coverage counts executed lines. Branch coverage counts whether both outcomes of each decision were taken, which makes it stricter, since a line with an if statement can be covered when only the true path runs.

Coverage reveals what was not tested, and this is its real value. It says nothing about whether tests would notice a bug. A test that calls a function and checks nothing gives 100% coverage of that function with no protection at all. For this reason, coverage should inform a team, and pushing it towards an arbitrary high number encourages low-value tests. Mutation testing, which deliberately alters code to see if tests fail, measures test strength more directly.

True or false

What coverage can and cannot tell you

Exam skills 3.2.4

Check what code coverage measures.

Log in to try this activity Interactive activities are free with an account.

Publishing coverage in Azure Pipelines

Test frameworks produce reports in standard formats, and Cobertura and JaCoCo XML are the most common. PublishCodeCoverageResults@2 reads them and adds a Code Coverage tab to the run. It accepts formats such as .coverage, .cjson, .xml and .lcov.

steps:
  - script: dotnet test --collect:"XPlat Code Coverage" --results-directory $(Agent.TempDirectory)/coverage
    displayName: Run tests with coverage

  - task: PublishCodeCoverageResults@2
    condition: succeededOrFailed()
    inputs:
      summaryFileLocation: $(Agent.TempDirectory)/coverage/**/coverage.cobertura.xml
      failIfCoverageEmpty: true

failIfCoverageEmpty catches the silent failure in which the tool produces no data and the pipeline still appears healthy. The task works in build pipelines only.

Scenario questions

Publishing coverage

Exam skills 3.2.4

Choose the settings for publishing coverage in Azure Pipelines.

Log in to try this activity Interactive activities are free with an account.

Coverage on pull requests

Azure Pipelines can report diff coverage on pull requests: the percentage of the lines changed by a pull request that tests exercise. A status check then appears on the pull request. The thresholds go in a file named azurepipelines-coverage.yml at the repository root.

coverage:
  status:
    comments: on
    diff:
      target: 70%

A branch policy can make that status check required. This is usually a better gate than a project-wide figure, because a team working in a legacy codebase with 30% coverage cannot raise the whole number quickly, but it can make sure that new code is tested.

In GitHub Actions, teams generate a report in a test step, then upload it as an artifact or publish it through a coverage service or action. A step can fail the job when the value is below a threshold, with test tool options such as Jest's coverageThreshold.

The following diagram compares two code coverage gates for a 10-year-old application with low coverage.

Gating coverage on a 10-year-old application with 22% line coverage. An 80% gate on the whole codebase would block all work for months or invite low-value tests. A diff coverage target of about 70% on new code, as a required pull request check, tests every change and raises overall coverage gradually.
The scenario is illustrative.
True or false

Diff coverage on pull requests

Exam skills 3.2.4

Check how pull request coverage is configured and enforced.

Log in to try this activity Interactive activities are free with an account.
Worked example

Choosing a coverage gate

A 10-year-old application has 22% line coverage. Management proposes failing every build below 80%. Developers object, as this would block all work for months. What is a better approach?

Apply the gate to new code: configure diff coverage with a target of, say, 70%, and make the status check required on pull requests. Each change is then tested, and overall coverage rises gradually. The proposed gate would stop delivery or encourage low-value tests written just to reach a number. Removing coverage reporting altogether would lose useful visibility.

Check yourself

Which is stricter, line coverage or branch coverage?

Show answer

Branch coverage, since it needs both outcomes of a decision to run. A line with an if can be covered by only one path.

Exam Tips

Remember for the exam

  • Coverage shows what ran, not what was verified.
  • Branch coverage is stricter than line coverage.
  • PublishCodeCoverageResults@2 reads reports such as Cobertura XML.
  • azurepipelines-coverage.yml sets diff coverage targets.
  • Gate on changed code rather than a project-wide percentage.