Skip to main content

sonar_get_coverage_gaps

Analyze code coverage gaps for a specific file. Returns uncovered code blocks optimized for LLM-assisted test generation.

Description

Identifies untested code blocks in a file, including lines with no coverage and lines with partial branch coverage. Designed to help AI assistants generate targeted test cases.

Parameters

ParameterTypeRequiredDefaultDescription
componentKeystringYes-SonarQube file key (e.g., project:src/file.ts)
minGapSizenumberNo1Minimum consecutive uncovered lines
includePartialBranchbooleanNotrueInclude partial branch coverage

Finding Component Keys

Component keys follow the format: {projectKey}:{filePath}

Example: my-project-a1b2c3d4:src/services/UserService.ts

You can find them in:

  • sonar_scan_project results
  • sonar_get_project_metrics output
  • SonarQube web UI

Usage

Basic Usage

Show me untested code in UserService.ts

Tool call:

{
"componentKey": "my-project:src/services/UserService.ts"
}

Larger Gaps Only

Show me significant coverage gaps (5+ lines)

Tool call:

{
"componentKey": "my-project:src/services/UserService.ts",
"minGapSize": 5
}

Lines Only (No Branch)

Show me completely untested lines

Tool call:

{
"componentKey": "my-project:src/services/UserService.ts",
"includePartialBranch": false
}

Response Format

COVERAGE GAP ANALYSIS
━━━━━━━━━━━━━━━━━━━━━

File: src/services/UserService.ts
Coverage: 67.3%
Uncovered Lines: 45
Partially Covered: 12

📍 GAP 1: Lines 45-67 (Error Handling)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Coverage: 0%
Type: UNCOVERED_BLOCK

Code:
──────────────────────────────────────────────────────
45 │ async processPayment(amount: number) {
46 │ try {
47 │ const result = await this.gateway.charge(amount);
48 │ return result;
49 │ } catch (error) {
50 │ if (error instanceof InsufficientFundsError) {
51 │ this.notifyUser('insufficient_funds');
52 │ throw new PaymentError('INSUFFICIENT_FUNDS');
53 │ }
54 │ if (error instanceof NetworkError) {
55 │ await this.retryQueue.add(amount);
56 │ throw new PaymentError('NETWORK_ERROR');
57 │ }
58 │ throw error;
59 │ }
60 │ }
──────────────────────────────────────────────────────

Suggested Tests:
• Test successful payment flow
• Test InsufficientFundsError handling
• Test NetworkError with retry queue
• Test unknown error propagation

📍 GAP 2: Lines 89-102 (refund method)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Coverage: 0%
Type: UNCOVERED_BLOCK

Code:
──────────────────────────────────────────────────────
89 │ async refund(transactionId: string) {
90 │ const transaction = await this.findTransaction(transactionId);
91 │ if (!transaction) {
92 │ throw new NotFoundError('Transaction not found');
93 │ }
94 │ if (transaction.refunded) {
95 │ throw new ValidationError('Already refunded');
96 │ }
97 │ return this.gateway.refund(transaction);
98 │ }
──────────────────────────────────────────────────────

Suggested Tests:
• Test successful refund
• Test transaction not found
• Test already refunded error

📍 PARTIAL BRANCH: Line 34
━━━━━━━━━━━━━━━━━━━━━━━━━━

Branches Covered: 1/3
Type: PARTIAL_BRANCH

Code:
──────────────────────────────────────────────────────
34 │ if (user.role === 'admin' || user.role === 'manager' || user.premium) {
──────────────────────────────────────────────────────

Missing Coverage:
• Branch: user.role === 'manager'
• Branch: user.premium

SUMMARY:
3 coverage gaps found
Estimated tests needed: 10-12
Potential coverage increase: +25%

Gap Types

TypeDescription
UNCOVERED_BLOCKMultiple consecutive uncovered lines
UNCOVERED_LINESingle uncovered line
PARTIAL_BRANCHSome branches not executed

Examples

Example 1: Find Untested Error Paths

Prompt:

What error handling code isn't tested?

Tool Call:

{
"componentKey": "my-project:src/api/handlers.ts",
"minGapSize": 3
}

Example 2: Generate Tests for Gaps

Prompt:

Show me coverage gaps in PaymentService so I can write tests

Tool Call:

{
"componentKey": "my-project:src/services/PaymentService.ts"
}

Example 3: Branch Coverage Focus

Prompt:

Which conditional branches aren't fully tested?

Tool Call:

{
"componentKey": "my-project:src/utils/validators.ts",
"includePartialBranch": true,
"minGapSize": 1
}

Best Practices

  1. Focus on critical paths - Test business logic first
  2. Cover error handling - Error paths often lack tests
  3. Complete branch coverage - Partial branches hide bugs
  4. Use AI assistance - Let AI generate test skeletons

Common Issues

"Component not found"

Cause: Invalid component key format.

Solution: Check project key and file path match exactly.

"No coverage data"

Cause: No coverage report uploaded to SonarQube.

Solution: Configure coverage reporting in your build.