Sobes.tech
Senior

What is test coverage and how can it be measured?

sobes.tech AI

Answer from AI

Test coverage is a metric that shows which part of the code is covered by automated tests. It helps assess the completeness of testing and identify untested areas.

Main types of coverage:

  • Line coverage — how many lines of code are executed during tests.
  • Branch coverage — how many logical branches (if/else) are tested.
  • Function coverage — how many functions are called.
  • Condition coverage — testing all conditions within expressions.

Coverage can be measured using tools such as:

  • Istanbul/nyc for JavaScript/TypeScript
  • JaCoCo for Java
  • Coverage.py for Python

These analyze test execution and generate reports with coverage percentages and file-level details.

Example command for measuring coverage with nyc:

nyc mocha

Where mocha is the test framework. The result is a report with coverage percentages.