PHPUnit Code Coverage

2020-07-03 04:24发布

问题:

I am learning the ropes with Unit testing Zend Framework applications. So far I have set up PHPUnit to work with Zend Framework and have started writing some simple Test Cases.

My issue is that I am wondering why Code Coverage does not work in spite of being set in the logging tag in my phpunit.xml.

I don't get any error but no coverage report is generated.

However it works when I run phpunit --coverage <dir>

The logging section of my phpunit is as below:

<phpunit bootstrap="./application/bootstrap.php" colors="true">
        <testsuite name="CI Test Suite">
            <directory>./</directory>
        </testsuite>
        <testsuite name="Library Test Suite">
            <directory>./library</directory>
        </testsuite>

        <filter>
            <whitelist>
                <directory suffix=".php">../application/</directory>
                <exclude>
                    <directory suffix=".phtml">../application</directory>
                    <file>../application/Bootstrap.php</file>
                    <file>../application/controllers/ErrorController.php</file>
                </exclude>
            </whitelist>
           <logging>
               <log type="coverage-html" target="./log/report" charset="UTF-8" yui="true"
   highlight="true" lowUpperBound="50" highLowerBound="80" />
               <log type="testdox" target="./log/testdox.html" />    
           </logging>
        </filter>
    </phpunit>

Anyone encounter this before? What is then likely problem?

回答1:

Here is the phpunit.xml for one of my projects, this works fine. As you can see, the logging section is outside the filter section, so that is probably your issue as commented by Mark Baker. I chose this one as it is from a small project and is very simple.

<phpunit bootstrap="./bootstrap.php" colors="false">
    <testsuite name="HSSTests">
        <directory>./</directory>
    </testsuite>

    <filter>
        <whitelist>
            <directory suffix=".php">d:/wamp/app_hss/</directory>
            <exclude>
                <directory suffix=".phtml">d:/wamp/app_hss/</directory>
                <directory suffix=".php">d:/wamp/app_hss/tests/</directory>
            </exclude>
        </whitelist>
    </filter>

    <logging>
        <log type="coverage-html" target="./log/codeCoverage" charset="UTF-8"
            yui="true" highlight="true"
            lowUpperBound="50" highLowerBound="80"/>
        <log type="testdox-html" target="./log/testdox.html" />
    </logging>
</phpunit>

All the information you could ever need on this is in the PHPunit manual.