← Back to libraryQuestion 247 of 273
📋Software TestingIntermediate

Test Coverage & Code Coverage

📌 Definition:

Test coverage measures what we've tested; Code coverage measures which code was executed during testing.

📖 Detailed Explanation:

Coverage is a quality metric. However, 100% coverage doesn't mean 100% quality. You could have 100% code coverage but still have bugs. But 0% coverage is definitely risky. Coverage shows testing completeness, not quality. The goal is to balance coverage with quality of tests.

📎 Test Coverage:
Definition:

Percentage of requirements or features that have been tested

Purpose:

Ensures all business requirements are validated

Calculation:

(Number of requirements tested / Total requirements) × 100

Example:

If product has 50 features and we tested 40 → 80% test coverage

📎 Code Coverage:
Definition:

Percentage of source code executed by tests

Purpose:

Ensures most/all code paths are exercised during testing

Types:
TypeDescriptionExample
Line Coverage (Statement Coverage)Percentage of code lines executedCode has 100 lines. Tests execute 80 lines. Coverage = 80%
Branch Coverage (Decision Coverage)Percentage of if/else/switch branches takenCode has if-else statement. Must test both if=true and if=false for 100% branch coverage
Path Coverage (Cyclomatic Complexity)Percentage of all possible execution pathsCode has 3 if-statements = 2³ = 8 possible paths. Must test all 8 for 100% path coverage
Function CoveragePercentage of functions that are called at least onceCode has 50 functions. Tests call 45 functions. Coverage = 90%
Calculation:

(Lines of code executed / Total lines of code) × 100

Tools:
  • JaCoCo (Java code coverage)
  • Istanbul (JavaScript/Node.js)
  • Coverage.py (Python)
  • Cobertura (Java/Python)
  • Clover (Enterprise Java)
📎 Coverage Goals By Industry:
LevelTarget
Critical/Safety-Critical100% code coverage - Even 1% untested code is risky (healthcare, aviation, banking)
High-Risk Financial/Payment95%+ code coverage - Payment systems demand high coverage
Standard Web/Mobile Apps70-80% code coverage - Reasonable balance of effort vs risk
Internal Tools/Low-Risk50%+ code coverage - Lower priority
📎 Typical Coverage Distribution:
Unit Tests:

70% of total code coverage (fast, cheap)

Integration Tests:

20% additional coverage (moderate cost)

System Tests:

10% additional coverage (expensive)

📎 Why 100 Percent Coverage Is Not Enough:
  • You could test all lines but miss edge cases
  • Coverage only shows code was EXECUTED, not that it's CORRECT
  • Some code is unreachable in practice (defensive coding)
  • High coverage + low-quality tests = false confidence
📎 Coverage Vs Quality Example:
Scenario:

Discount calculation function

Bad Test:

Test with 100 normal cases, achieve 100% coverage, but all pass. Function has logic bug (discount > 100% not handled)

Good Test:

Test with 20 cases including edge cases (0% discount, 100%+ discount, null values). Coverage 80% but catches bugs.

🎯 Scenario-Based Interview Question:

You have 70% code coverage. Your manager wants 80%. You need to add 20 new test cases to get there. How do you prioritize which new tests to write?

💡 Quick Tip:

📊 100% coverage ≠ 100% quality. High coverage with quality tests > Low coverage with quality tests. Aim for 70-80% with good tests.

💡 Interview Focus:

Interviewers want to see you understand that coverage is a metric, not a goal. Quality > Quantity.