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
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
componentKey | string | Yes | - | SonarQube file key (e.g., project:src/file.ts) |
minGapSize | number | No | 1 | Minimum consecutive uncovered lines |
includePartialBranch | boolean | No | true | Include 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_projectresultssonar_get_project_metricsoutput- 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
| Type | Description |
|---|---|
UNCOVERED_BLOCK | Multiple consecutive uncovered lines |
UNCOVERED_LINE | Single uncovered line |
PARTIAL_BRANCH | Some 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
- Focus on critical paths - Test business logic first
- Cover error handling - Error paths often lack tests
- Complete branch coverage - Partial branches hide bugs
- 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.
Related Tools
sonar_get_project_metrics- Overall coveragesonar_scan_project- Get file keyssonar_get_issue_details- Related issues