New in 4.0: Code Coverage for PHPUnit

We continue developing integration with tools essential for each developer who cares about the quality of his code. This time we want to introduce you Code Coverage for PHPUnit. What is Code Coverage for PHPUnit? It shows you how much of your code is covered with tests and marks covered and uncovered lines visually right in the editor.

Code coverage metrics in 'Project View'

Covered and uncovered line marks in the editor

Let’s try to get your first code coverage measurement in PhpStorm!

  1. Enable Xdebug or Zend Debugger extension in your PHP interpreter configuration (see Xdebug installation guide and Zend Debugger installation guide).
  2. Choose an existing ‘PHPUnit’ run configuration or create a new one.
  3. Launch the run configuration using the new ‘Coverage’ button.

‘Coverage’ button in ‘Run’ menu

That is all!

Download PhpStorm & WebStorm 4.0 EAP and don’t forget to provide feedback in the issue tracker ;)

Develop with pleasure!

This blog is permanently closed.

For up-to-date information please follow to corresponding WebStorm blog or PhpStorm blog.

 
This entry was posted in Cool Feature, PhpStorm and tagged , , . Bookmark the permalink.

23 Responses to New in 4.0: Code Coverage for PHPUnit

  1. Patrick says:

    Great feature! I’ll upgrade to 4.0 these days

    All features sounds really great :-)

  2. Andriy Bazanov says:

    Very good — thanks Nikolay. Too bad it does not work on Windows (at least for me) — WI-9418 .

    Few questions:
    1) How to remove those (XX% lines covered) if I do not want to see them any more (without re-opening project). There should be some “switch” available (I think IDEA has it, although unsure on this one).
    2) Is there possibility to display such (XX% lines covered) for source files only, and not for the test files itself? Right now *all* files/folders have that text, despite the fact that my “tests” folder is marked as Test Source Root. Maybe it is worth bringing “Source Root” mark from IDEA (blue folder icon, so only files in this folder and subfolders will have that coverage text)?
    3) Is there possibility to have whole line highlighted not just gutter? At least temporarily? Much easier to see those tested/untested lines.

    • Alexey Gopachenko says:

      Good points. File all as feature requests for 4.0 :)
      And the one below as a problem :)

      • Andriy Bazanov says:

        1) I have found such action (actually 2) using Help | Find Action:
        – 1.1) Hide coverage information (has no shortcut) and is from 1.3 panel
        – 1.2) Show Code Coverage Data (Ctrl+Alt+F6). It is NOT listed anywhere in main menu, so I had to use “Include non-menu actions” option, which most people simply forgetting to use. In any case — I think this action should be available in main menu somewhere. I think about submitting a ticket about this…
        - 1.3) By clicking on Coverage marker in editor gutter — it shows some small poup window, which has that action from 1.1 (although by default it is hidden, as popup is too narrow, and only fits 3 out of 4 buttons). Ticket is coming…

        2) Originally I’ve tested it using only built-in Coverage report. because such coverage.xml file is not generated properly ( WI-9418 ) it displayed that “XX% lines covered” test for all folders. Later, at home, I have discovered that I can generate proper coverage.xml with phpunit itself and provide that file to PhpStorm via “Show Code Coverage Data” screen — now my tests files do not have that coverage text next to them. This point is now obsolete (as far as I’m concerned) as once that aforementioned issue will be resolved, it should be good (at least that’s what I’m expecting).

        3) WI-9907

        • nnmatveev says:

          Thanks for constructive feedback! 1.1 and 1.2 – fixed. See ‘Tools’ menu in the next EAP. 1.3 – assigned to the responsible developer (not me).

  3. Andriy Bazanov says:

    If my PHPUnit xml config defines custom clover coverage file and PHPUnit test is run with Coverage button, then my coverage.xml is not generated. I think you should make this moment clear as I think there will be questions “where is MY coverage.xml” in the future (I do not think you can safely rely on using my own coverage.xml as it may be deleted at any time, while your one will still be available (because of where it is stored)).

    • nnmatveev says:

      Our Xdebug code coverage based on PHPUnit functionality – we just pass ‘–coverage-clover’ option through command line (with path to our internal output file). If you have this option in your configuration file it will be overridden. We can’t do anything with that.

      • Andriy Bazanov says:

        I understand that, that’s why I suggested to make this moment clear in documentation (or somewhere else), otherwise you may hear these “where is *MY* coverage.xml file” type of questions.

  4. Andriy Bazanov says:

    Have few more questions (will ask one at a time).
    View | Tool windows | Coverage — is that the same as Coverage popup when clicking on coverage marker in editor gutter .. or something else? I’m asking because it is seems to be disabled all the time.

  5. Bee waiting for this for a while since xdebug doesn’t play well with zend on windows and PHPUnit needed it for code coverage. Going to test it out. Thanks for the new feature

  6. OK I so I actually tested this out but it isnt showing any lines as covered even though they are being run and tests are passing. Everything shows as uncovered and the gutter is red for all the methods. Is there an clover.xml I am supposed to point it to or a PHPUnit report I am supposed to generate in the configuration file I am using Windows

  7. Sebastiaan Stok says:

    Great!

    One little suggestion, PHPUnit supports creating an HTML coverage report including the Change Risk Analysis and Predictions.
    http://sebastian-bergmann.de/archives/877-CRAP-in-PHPUnit-3.5.html

    It would be great to also see this the result :)

    Keep up the good work.

  8. Rene says:

    When PHPUnit terminates because of memory exhaustion PHPStorm would not report it but instead simply omit all coverage info. No coverage data is being shown then. FYI

    • As the very quick solution, try to comment out verification of uncovered files in code coverage. It works for big projects like magento and symfony.

      line #170
      /usr/share/php/PHP/CodeCoverage.php

      /**
      * Returns the collected code coverage data.
      *
      * @return array
      * @since Method available since Release 1.1.0
      */
      public function getData()
      {
      /*
      if ($this->processUncoveredFilesFromWhitelist) {
      $this->processUncoveredFilesFromWhitelist();
      }

      // We need to apply the blacklist filter a second time
      // when no whitelist is used.
      if (!$this->filter->hasWhitelist()) {
      $this->applyListsFilter($this->data);
      }
      */
      return $this->data;
      }

  9. aimfeld says:

    Does not work for me (Windows 7, PHPStorm 4.0.1, Zend_Debugger). I can debug my tests perfectly but the coverage results show all code lines as uncovered (red bars on the left of the editor). The debugger stops on breakpoints set on these lines, though.

    The code coverage window opens and shows my folders after running code coverage. The title of the window says “Coverage Summary: null” and the “Statistics %” column is empty.

    Is it even supposed to work with Zend_Debugger or do I need Xdebug? Xdebug is listed as a requirement here: https://github.com/sebastianbergmann/php-code-coverage

    Any suggestions?

  10. Josef van Niekerk says:

    Please refer to this StackOverflow question.

    I have no idea how to get code coverage to show up in my Symfony2 project. Any ideas?

  11. Pingback: PhpStorm - Dương Quốc Việt Blog

Comments are closed.