This Question is Not Answered

1 "correct" answer available (4 pts) 1 "helpful" answer available (2 pts)
2 Replies Last post: Mar 8, 2013 11:45 AM by Laurent POLESE  
Laurent POLESE Newbie 24 posts since
Sep 30, 2011
Currently Being Moderated

Mar 7, 2013 9:47 PM

PHPUnit code coverage : wrong files coverage rate

Hello,

 

I'm not sure this is about PhpStorm, but maybe someone here could help.

We're just beginning to use unit testing with PHPUnit / PhpStorm, and we're facing an issue.

Our test classes are next to tested classes in our file system.

In my example, I have lib/Chrono.php and lib/ChronoTest.php

Phpstorm file coverage probleme.png

But we didn't manage to exclude test files from coverage metrics for data displayed in the Project view.

So the test files are counted in total files in each folder, which displays 50% coverage while 100% are covered.

 

This is a real issue for us, because we aim 100% file coverage and wanted to rely on PhpStorm to get this information.

 

Here is my PHPUnit xml file

<phpunit bootstrap="CONFIG.php" colors="true">

    <testsuite name="Test Laurent conf Fred">

        <directory suffix="Test.php">lib</directory>

    </testsuite>

    <filter>

        <whitelist>

            <directory suffix=".php">lib</directory>

            <exclude>

                <directory suffix="Test.php">lib</directory>

            </exclude>

        </whitelist>

    </filter>

    <logging>

        <log type="coverage-html" target="./codeCoverage" charset="UTF-8"

             yui="true" highlight="false"

             lowUpperBound="35" highLowerBound="70"/>

        <log type="coverage-clover" target="./codeCoverage/codeCoverage.xml"/>

        <log type="metrics-xml" target="./metrics/metrics.xml"/>

        <log type="test-xml" target="./testResults/logfile.xml" logIncompleteSkipped="false"/>

    </logging>

</phpunit>

 

I tried many combinations with excludes, blacklists but nothing worked.

 

Any idea ? Thanks a lot

Laurent

Andriy Bazanov Master 2,645 posts since
May 28, 2010
Currently Being Moderated
Mar 7, 2013 11:40 PM in response to: Laurent POLESE
Re: PHPUnit code coverage : wrong files coverage rate

Hi Laurent,

 

#1

So the test files are counted in total files in each folder, which displays 50% coverage while 100% are covered.

 

This is a real issue for us, because we aim 100% file coverage and wanted to rely on PhpStorm to get this information.

You only reliable option is to PROPERLY separate tested classes from actual tests (this will work fine in both any IDE and PHPUnit itself).

 

Shortly speaking: tested classes in one folder and tests in another (which should be marked as Test Source Root in "Settings | Directories")

 

Example:

[MyProject]

|-- [lib]

|  |-- [MyFramework]

|    |-- Chrono.php

|-- [tests]

|  |-- [MyFramework]

|  |  |-- ChronoTest.php

|  |-- [assets] (resources/files used during testing -- to check if tests doing well, e.g. expected output)

|  |-- [reports] (for generated reports; e.g. HTML code coverage)

|  |-- [var] (for temp files created during tests execution)

|  |-- bootstrap.php (bootstrap file executed before running phphunit tests -- used to initialise require classes/autoloading etc)

|  |-- phpunit.xml (phpunit configuration file)

|-- build.xml (phing build file -- to cleanup temp/reports folders etc)

 

Of course, you are free to submit Feature / Bug ticket to the Issue Tracker (if it is not there already) and it may get fixed .. but separating tests from actual classes is more correct approach (and will work everywhere).

 

#2 -- If you are referring to wrong percentage for specific file (fro your screenshot: "98% lines") -- it's either

  • PHPUnit's own bug (I guess) -- when it generates coverage clover.xml file, it write a bit different/wrong values;
  • or PhpStorm's way of calculating it (PhpStorm uses ceratin fields/values from coverage clover.xml file as is while PHPUnit's own HTML report is either uses different fields or applies some extra logic)

 

In any case: http://youtrack.jetbrains.com/issue/WI-9910

More Like This

  • Retrieving data ...