Study guide
8.4 Code Coverage Analysis & SonarQube Integration
Microsoft Certified: Azure DevOps Engineer Expert AZ-400
Track your progress as you read
Finished sections get a check mark.
1 Chapter 1: Flow of Work, Traceability & Work Tracking
1.1 Flow of Work, GitHub Flow & Feedback Cycles 1.2 Work Tracking with Azure Boards & GitHub Projects 1.3 End-to-End DevOps Traceability & Quality Tracking
2 Chapter 2: DevOps Metrics, Dashboards & Team Collaboration
2.1 DORA Metrics, Flow Metrics & Dashboard Design 2.2 Lifecycle Metrics Across the Six DevOps Phases 2.3 Work Item Queries, WIQL Charts & Analytics 2.4 Project Documentation: Wikis, Mermaid Diagrams & Release Notes 2.5 Webhooks, Service Hooks & Microsoft Teams ChatOps
3 Chapter 3: Git Branching Strategies & Pull Request Workflows
3.1 Git Branching Models & Strategy Design 3.2 Pull Request Workflows & Merge Strategies 3.3 Branch Policies, Quality Gates & Compliance Restrictions
4 Chapter 4: Repository Management, Optimization & Git Data Operations
4.1 Git LFS Architecture & .gitattributes Tracking 4.2 LFS History Migration, git-fat & File Locking 4.3 Git Repository Scaling, Scalar & Monorepo Governance 4.4 Repository Permissions & Git Tag Governance 4.5 Git Data Recovery & Sensitive-Data Scrubbing
5 Chapter 5: Package Management & Dependency Versioning
5.1 Selecting Package Tools: Azure Artifacts vs. GitHub Packages 5.2 Feed Scoping, Upstream Sources & Pipeline Authentication 5.3 Package Views, Immutability & Quality Promotion 5.4 Dependency Versioning: SemVer, CalVer & Automated Versioning 5.5 Pipeline Artifact Versioning & Artifact Storage Strategy
6 Chapter 6: Pipeline Infrastructure, Agents & Integration
6.1 Selecting a CI/CD Solution: Azure Pipelines vs. GitHub Actions 6.2 GitHub-Azure Pipelines Integration & Hybrid Migration 6.3 Hosted vs. Self-Hosted Agents & Runners 6.4 VMSS Elastic Pools, Container Jobs & Agent Governance 6.5 Pipeline Triggers, Filters & Concurrency Management
7 Chapter 7: YAML Pipeline Architecture & Reusable Elements
7.1 YAML Pipeline Anatomy: Stages, Jobs, Steps & Workspaces 7.2 Job Execution Order: DAG Dependencies, Parallelism & Conditions 7.3 Reusable Elements: Templates, Parameters & Variables 7.4 Variable Groups, Secret Variables & Key Vault Linking 7.5 YAML Environments, Checks & Deployment Approvals
8 Chapter 8: Testing Strategy, Quality Gates & Code Coverage
8.1 Test Strategy Design: Local, Unit & Integration Testing 8.2 Load, Stress & Performance Testing in Pipelines 8.3 Test Execution Tasks, Agents & Results Integration 8.4 Code Coverage Analysis & SonarQube Integration 8.5 Quality Gates, Release Gates & Governance Checks
9 Chapter 9: Modern Deployment Strategies & Zero-Downtime Delivery
9.1 Progressive Delivery: Blue-Green, Canary & Ring Deployments 9.2 Minimizing Downtime: Deployment Slots & Rolling Updates 9.3 Feature Flags with Azure App Configuration & Feature Manager 9.4 Designing the High-Priority Hotfix Path 9.5 Ordered Dependency Deployments, Resiliency & Rollback
10 Chapter 10: Artifact Deployment & Database DevOps
10.1 Containerized Application Delivery: ACR, AKS & Container Apps 10.2 Binary, Web App, Function & Scripted Deployments 10.3 Database DevOps: State-Based & Migration-Based Strategies
11 Chapter 11: Infrastructure as Code (IaC) & Azure Deployment Environments
11.1 Infrastructure as Code Strategy: Bicep, ARM & Terraform 11.2 Desired State Configuration & Azure Machine Configuration 11.3 Azure Deployment Environments (ADE) & Developer Self-Service
12 Chapter 12: Pipeline Maintenance, Health & Modernization
12.1 Pipeline Health, Flaky Tests & Reliability Engineering 12.2 Pipeline Optimization: Caching, Slicing & Concurrency 12.3 Artifact & Build Retention Governance 12.4 Modernizing CI/CD: Classic to YAML Migration
13 Chapter 13: Authentication, Authorization & Identity Governance
13.1 Microsoft Entra Service Principals & Managed Identities 13.2 Service Connections, Workload Identity & Personal Access Tokens 13.3 Azure DevOps Projects, Teams & Permission Evaluation 13.4 Access Levels, GitHub Roles & Outside Collaborators
14 Chapter 14: Secrets Management, Sensitive Data & DevSecOps Scanning
14.1 Azure Key Vault, Secret Variables & Secretless CI/CD 14.2 Security Scanning Strategy, GHAS & Secret Scanning 14.3 CodeQL SAST in Pipelines and Containers 14.4 Software Supply Chain, Container Scanning & Cloud Defender
15 Chapter 15: Telemetry Collection, Azure Monitor & Alerting
15.1 Azure Monitor Architecture, AMA & Data Collection Rules 15.2 Application Insights & Specialized Azure Monitor Insights 15.3 Alerting Rules, Action Groups & GitHub Repository Insights
16 Chapter 16: Telemetry Analysis, Distributed Tracing & SRE Practices
16.1 Distributed Tracing, Application Map & Exception Triage 16.2 KQL Log Analytics, SRE Observability & Error Budgets
8.4 Code Coverage Analysis & SonarQube Integration
Key Takeaways Explain This Topic AI
Line coverage counts executed statements while branch coverage counts decision paths; branch coverage is the stricter and more meaningful metric.
PublishCodeCoverageResults@2 accepts Cobertura and JaCoCo formats and renders the Code Coverage tab on the pipeline run.
The SonarQube sequence is Prepare, then the build, then Analyze, then Publish - running Analyze before the build produces no results.
Clean as You Code sets conditions on new code only, so a legacy codebase can adopt a strict gate without a large remediation project first.
Pull request decoration posts SonarQube findings as PR comments, and the quality gate status becomes a required branch policy check.
Last updated: September 2026
8.4 Code Coverage Analysis & SonarQube Integration
High-performing engineering organizations do not rely on manual inspection or developer goodwill to enforce code quality and security standards. Instead, they implement automated quality gates that act as programmable barriers throughout the CI/CD lifecycle. Code that fails coverage thresholds, contains known security vulnerabilities, or causes infrastructure telemetry anomalies is automatically blocked from merging or deploying.
On the AZ-400 certification exam, candidates must master three interconnected quality governance pillars:
Code Coverage Collection & Publishing (Coverlet, JaCoCo, Cobertura).
Static Code Analysis & Quality Gate Enforcement (SonarQube, SonarCloud, Pull Request Status Checks).
Automated Release Gates & Environment Checks (Azure Monitor Alerts, REST API verification, Work Item queries).
1. Code Coverage Fundamentals & Metrics
Code coverage measures the degree to which source code is executed when an automated test suite runs. It highlights untested code paths, dead code, and areas of high regression risk.
Core Coverage Metrics
Line / Statement Coverage : Measures whether each executable line of code was touched by at least one test. While popular, high line coverage can create false confidence; a line with a complex conditional statement can execute without evaluating all logical branches.
Branch / Decision Coverage : Measures whether each branch of control structures ( if , else , switch , case , ternary operators) was executed in both its true and false states. This is the industry standard metric for robust testing.
Condition Coverage : Measures whether every boolean sub-expression within a composite condition (e.g., if (A && B || C) ) has been evaluated to both true and false independently.
Method / Function Coverage : Measures the percentage of defined methods invoked during testing.
Popular Coverage Collection Tools
.NET : Coverlet (cross-platform collector for .NET Core/.NET 8+), Visual Studio Coverage ( vstest ).
Java : JaCoCo (Java Code Coverage library), Cobertura.
JavaScript/TypeScript : Istanbul / nyc , c8.
Python : Coverage.py , pytest-cov .
2. Publishing and Visualizing Code Coverage in Azure Pipelines
Azure Pipelines standardizes on two open coverage XML formats: Cobertura and JaCoCo .
The PublishCodeCoverageResults@2 Task
To visualize coverage metrics in the Azure DevOps portal, pipelines invoke the PublishCodeCoverageResults@2 task: # .NET 8 Test Execution and Coverage Publishing
- task: DotNetCoreCLI@2
- displayName: 'Run Tests and Collect Coverage'
inputs:
- command: 'test'
- projects: '**/*Tests/*.csproj'
- arguments: >-
- -configuration Release
- -collect:"XPlat Code Coverage"
- - DataCollectionRunSettings.DataCollectors.DataCollector.Configuration.Format=cobertura
- task: PublishCodeCoverageResults@2
- displayName: 'Publish Code Coverage Results to Pipeline'
inputs:
- summaryFileLocation: '$(Agent.TempDirectory)/**/coverage.cobertura.xml'
- pathToSources: '$(Build.SourcesDirectory)' # Enables source file code line highlighting
- What the Code Coverage Tab Displays
When PublishCodeCoverageResults@2 completes:
Summary Widget : Displays aggregate Line Coverage and Branch Coverage percentages on the main build summary page.
Code Coverage Tab : Provides an interactive directory-tree explorer showing coverage metrics broken down by assembly, namespace, class, and source file.
Source Highlighting : If pathToSources is provided, clicking into a source file reveals color-coded lines indicating fully covered lines (green), partially covered branches (yellow), and unexecuted lines (red).
3. SonarQube & SonarCloud Integration Architecture
While code coverage measures test volume, SonarQube (self-hosted server) and SonarCloud (cloud-managed SaaS) analyze code quality, maintainability, and security vulnerabilities. [Azure Pipelines YAML Build]
- │
- 1. SonarQubePrepare@5 (Configure)
- │
- 2. Build & Test (Compile + Run Tests)
- │
- 3. SonarQubeAnalyze@5 (Static Scan)
- │
- 4. SonarQubePublish@5 (Quality Gate Status)
- │
- ┌───────────────────────┴───────────────────────┐
- ▼ ▼
- [SonarQube Server / Cloud] [Azure DevOps PR Summary]
- Scans 30+ Languages • Posts PR Comments
- Evaluates Clean as You Code • Reports Quality Gate Status Check
- Calculates Technical Debt • Blocks PR Merge if Gate Fails
- The Canonical Four-Task SonarQube Sequence
Integrating SonarQube into an Azure Pipelines YAML build requires a strict sequence of tasks. Changing this order causes analysis failures: steps:
- # Step 1: Prepare Analysis Configuration (MUST run before build/compile!)
- task: SonarQubePrepare@5
- displayName: 'Prepare SonarQube Analysis'
inputs:
- SonarQube: 'SonarQube-ServiceConnection' # Service connection endpoint
- scannerMode: 'MSBuild' # Options: MSBuild, CLI, Other
- projectKey: 'Contoso_Banking_PaymentService'
- projectName: 'PaymentService'
- extraProperties: |
- sonar.cs.opencover.reportsPaths=$(Agent.TempDirectory)/**/coverage.opencover.xml
- sonar.exclusions=**/Migrations/**,**/bin/**,**/obj/**
- # Step 2: Build the Application
- task: DotNetCoreCLI@2
- displayName: 'Compile Solution'
inputs:
- command: 'build'
- projects: '**/*.sln'
- arguments: '--configuration Release'
- # Step 3: Run Tests and Generate Coverage
- task: DotNetCoreCLI@2
- displayName: 'Execute Tests'
inputs:
- command: 'test'
- projects: '**/*Tests/*.csproj'
- arguments: '--configuration Release --collect:"XPlat Code Coverage"'
- # Step 4: Run Code Analysis (Uploads AST, metrics, and coverage to SonarQube)
- task: SonarQubeAnalyze@5
- displayName: 'Run SonarQube Code Analysis'
- # Step 5: Publish Quality Gate Result (Blocks pipeline if gate fails)
- task: SonarQubePublish@5
- displayName: 'Publish Quality Gate Result'
inputs:
pollingTimeoutSec: '300' # Waits up to 5 minutes for SonarQube background worker
Important
Critical AZ-400 Sequence Rule :
When using the MSBuild scanner mode (for .NET and C++), SonarQubePrepare must execute before the build step , because it hooks into the Roslyn compiler pipeline to track compilation artifacts. Conversely, SonarQubeAnalyze must execute after the build and test steps so it can parse compiler output, test result files, and coverage reports.
- 4. SonarQube Quality Gates & Pull Request Decoration
- The "Clean as You Code" Paradigm
Enterprise codebases often suffer from years of accumulated legacy code and technical debt. Requiring an old monolith to achieve 85% overall code coverage before allowing any deployment is unrealistic and paralyzes delivery.
SonarQube solves this through Clean as You Code , establishing quality gates strictly on New Code (the "leak period", typically defined as code introduced in a pull request or since the previous release version):
- Quality Gate Metric on New Code Standard Recommended Threshold
- Coverage on New Code ≥ 80.0 % \ge 80.0\% ≥ 80.0%
- Duplicated Lines on New Code ≤ 3.0 % \le 3.0\% ≤ 3.0%
- Maintainability Rating on New Code A (Technical debt ratio < 5 % < 5\% < 5% )
- Reliability Rating on New Code A (0 New Bugs)
- Security Rating on New Code A (0 New Vulnerabilities)
- Security Hotspots Reviewed 100 % 100\% 100% Reviewed
- Pull Request Decoration and Branch Policy Enforcement
To enforce this gate automatically on every pull request:
PR Decoration : SonarQube connects to Azure Repos via a Personal Access Token (PAT). As the pipeline analyzes the PR branch, SonarQube decorates the pull request with line-by-line comments detailing code smells and security flaws.
Status Check Policy : In Azure DevOps, navigate to Project Settings → Repositories → Select Repo → Policies → Select main branch → Status Checks .
Add a required status check for SonarQube/quality gate (or SonarCloud/quality gate ).
If the SonarQube analysis determines that new code coverage is 72% (below the 80% threshold) or introduces a SQL injection flaw, the status check fails, and the Azure Repos pull request merge button is completely disabled until the developer resolves the issues.