Sobes.tech
Middle

What level of test coverage is considered normal?

sobes.tech AI

Answer from AI

There is no universal "normal" test coverage percentage. The optimal level depends on many factors:

  • Application criticality: Financial systems, medical software, and high-risk applications require higher coverage than, for example, personal blogs.
  • Code complexity: Complex algorithms and logic require more detailed testing.
  • Frequency of changes: A project that changes often benefits from a more comprehensive test suite to detect regressions.
  • Types of tests: Coverage can be measured by lines, branches, functions, etc. Line coverage of 70% may be acceptable, whereas branch coverage of 50% may indicate missed scenarios.
  • Team culture: Teams with a strong quality culture strive for higher coverage.

More important is the value of tests rather than just the percentage coverage. Good tests verify key functionality, boundary cases, and potential errors. A high coverage percentage is useless if the tests are invalid or do not check important aspects.

Common ranges include:

  • 50-70%: Basic coverage, acceptable for less critical or rapidly changing projects.
  • 70-90%: Good level, covers most main scenarios and boundary cases.
  • 90% and above: High coverage, often found in critically important systems or libraries.

Instead of aiming for a magical number, focus on:

  • Understanding what your tests verify.
  • Testing critical parts of the application.
  • Writing tests that genuinely catch errors.
<?php

// Example of a basic unit test for an addition function
use PHPUnit\Framework\TestCase;

final class CalculatorTest extends TestCase
{
    public function testAdd()
    {
        $calculator = new Calculator();
        $this->assertEquals(5, $calculator->add(2, 3)); // Check the basic case
    }

    public function testAddWithNegativeNumbers()
    {
        $calculator = new Calculator();
        $this->assertEquals(1, $calculator->add(-2, 3)); // Check the boundary case
    }
}

class Calculator
{
    public function add($a, $b)
    {
        return $a + $b;
    }
}

Conclusion: there is no single "normal" value. The goal is not to reach a percentage but to achieve effective test coverage that reduces risks and increases confidence in the code quality.

What level of test coverage is considered normal? — PHP - sobes.tech