PhpStorm 2021.1 Help

Code coverage

Code coverage in PhpStorm allows you to perform on-the-fly line coverage measuring for your code with low runtime overhead. In general, line coverage answers the question, "Was this line of code executed during unit testing simulation?"

Measuring code coverage is available for PHPUnit tests, Mocha tests, and Karma tests.

Code coverage results are displayed in the Coverage tool window, in the Project view of the Project tool window, and in the editor. The tool windows show the following information:

  • For a directory: the percentage of the covered classes and lines.

  • For a file: the percentage of the covered lines.

When a file is opened in the editor, each line is highlighted with regard to its code coverage status:

  • Lines executed during simulation are marked green.

  • Lines not executed during simulation are marked red.

The coverage measurement results comprise a coverage suite. You can have the results of a new simulation merged with any existing suite. In this case, a line will be considered covered if it is covered by at least one of the simulations.

A coverage suite is generated every time a test or application with code coverage measurement is executed. It is possible to have an unlimited amount of coverage suites.

Run with code coverage

General steps for using code coverage in a project

  1. Configure the code coverage runner. In PhpStorm, you can use any of the following code coverage runners:

    • Xdebug, a debugging extension providing both the debugging and code coverage capabilities.

      To enable Xdebug, make sure at least the following settings are specified in the [xdebug] section of the active php.ini file.

      [xdebug] zend_extension="<path to xdebug extension>" xdebug.coverage_enable=1
      [xdebug] zend_extension="<path to xdebug extension>" xdebug.mode=coverage

      See Configure Xdebug for details.

    • PCOV, a lightweight extension only providing code coverage capabilities.

      To enable PCOV, download and install the extension as described in the PCOV documentation. Then, make sure at least the following settings are specified in the active php.ini file:

      extension="<path to pcov extension>"
    • phpdbg, a debugging and code coverage module, which is bundled in PHP 5.6 and later and requires no additional configuration.

  2. Specify how you want to process the coverage results.

  3. Create tests for the target code, if you are going to measure code coverage for testing.

  4. Configure code coverage measurement in the desired run/debug configuration.

  5. Run with coverage, using the dedicated command from the main menu Run | Run with Coverage, or click the Run with Coverage button the Run with Coverage button.

    Note that this will run the tests using the Xdebug code coverage runner. To use a different runner, you need to create a dedicated PHPUnit run/debug configuration.

  6. Once the run with coverage has been executed, you can perform the following actions:

Last modified: 27 August 2021